PUT /api/v1/departments/15
Basic rules:
- You must use HTTP PUT method to update a resource. Optionally, you can create a resource if it does not exist.
- End point should be single (if not exceptional scenario e.g. Bulk update)
- Proper http response codes must be returned
- Do not add parameters to url, use body instead. Exposing data in url is a risk.
Http response codes to return
HTTP Code | When to return |
---|---|
200 - OK | When api updates a complete resource successfully. |
400 - Bad request | When api fails to update a resource because request sent by client in either invalid or incomplete. E.g. Request does not have values for mandatory properties or alphabets are sent in place of numeric values. |
404 - Not found | When resource you are looking for does not exist (If you do not support resource creation. You may create resource if it does not exist, in such case don't return 404) |
Bulk Update
Rarely you may want to update resources in bulk or batch to avoid round trips. In this case, you need to deal with few more http response codes -HTTP Code | When to return |
---|---|
207 - Multi Status | When api tries to update multiple resources then it may happen that few resources succeed and few fail. In such cases, api can return detailed status for each resource. These detailed status can be OK, Bad request (any one from above table) |
202 - Accepted | ASYNC? if you do not want client to wait for your response and start update process in background then its good to return Accepted so that client gets feedback to not to wait further.
Its a good practice to validate request so that you can ensure successful modification of resources
|
No comments:
Post a Comment