Get ready for your next interview with our comprehensive question library
REST (Representational State Transfer) is an architectural style for designing networked applications. The core principles are:
REST uses resources identified by URIs and standard HTTP methods to perform operations.
A RESTful API follows these characteristics:
/users/123)Example of RESTful endpoints:
GET /users # Get all users
GET /users/123 # Get specific user
POST /users # Create new user
PUT /users/123 # Update user
DELETE /users/123 # Delete user
Resources are the key abstraction in REST. A resource is any information that can be named and addressed. Resources should be:
/users not /getUsers/users/123/orders/456/users, not /user)Resources represent entities in your domain model and should map to business objects or data entities.
POST:
POST /users creates a new userPUT:
PUT /users/123 creates or updates user with ID 1232xx Success:
4xx Client Error:
5xx Server Error:
Authentication: Verifies who the user is (identity verification)
Authorization: Determines what the authenticated user can do (permission checking)
Both are typically required for secure APIs. Authentication happens first, then authorization checks permissions.
Return 422 Unprocessable Entity with detailed field-level errors:
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Validation failed",
"details": [
{
"field": "email",
"code": "INVALID_FORMAT",
"message": "Email must be valid format"
},
{
"field": "password",
"code": "TOO_SHORT",
"message": "Password must be at least 8 characters"
}
]
}
}
This helps clients understand exactly what needs to be fixed.
CORS (Cross-Origin Resource Sharing) allows controlled access to resources from different domains.
CORS Headers:
Access-Control-Allow-Origin: https://example.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Max-Age: 3600
Preflight Requests: Browser sends OPTIONS request for complex requests.
Security Considerations:
* for credentials-enabled requestsHATEOAS (Hypermedia as the Engine of Application State) means that API responses should include links to related actions or resources. This makes APIs self-discoverable and reduces client-server coupling.
Example response with HATEOAS:
{
"id": 123,
"name": "John Doe",
"email": "john@example.com",
"_links": {
"self": "/users/123",
"orders": "/users/123/orders",
"edit": "/users/123",
"delete": "/users/123"
}
}
Benefits include improved API discoverability, reduced documentation needs, and easier API evolution.
PUT: Replaces the entire resource with the provided data. If you omit fields, they should be set to default/null values.
PATCH: Partial update of a resource. Only updates the fields provided in the request body.
Example:
PUT /users/123
{
"name": "John Doe",
"email": "john@example.com"
}
# Replaces entire user object
PATCH /users/123
{
"email": "newemail@example.com"
}
# Only updates email field
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime