REST API
Multiple Endpoints: A REST API usually has different endpoints for different resources (e.g.,
/users
,/orders
).Fixed Data Structure: The server determines the structure and amount of data returned, which can sometimes result in over-fetching or under-fetching.
Predefined Responses: Each endpoint returns a fixed set of data, and customizing the response requires modifying the endpoint or adding new one
Less Flexible: Clients typically receive a full resource representation, and changes to the data format may require updates to the server or API versioning.
Multiple Requests: Nested or related data often requires multiple API calls, potentially leading to performance inefficiencies.
Versioning Required: REST often uses versioning (e.g.,
/api/v1/
) to manage changes and avoid breaking existing clients. This can lead to version sprawl over time.Separate Error Responses: Errors are usually returned in different HTTP status codes (e.g.,
404
,500
), making it simpler but also less flexible in terms of combining partial data with errors.No Type System: REST APIs do not inherently include a type system. JSON Schema or similar tools can be used, but they are not part of the REST standard.
REST API Example
Endpoint 1:
/users/1
(to get user details)Endpoint 2:
/users/1/posts
(to get the user's posts)
Request 1 (Get user details):
GET /users/1
Response:
{
"id": 1,
"name": "John Doe",
"email": "[email protected]"
}
Request 2 (Get user's posts):
GET /users/1/posts
Response:
[
{
"postId": 101,
"title": "My first post",
"content": "This is the content of my first post."
},
{
"postId": 102,
"title": "Another day, another post",
"content": "Here's some more content."
}
]
Last updated
Was this helpful?