til

Today I Learned: collection of notes, tips and tricks and stuff I learn from day to day working with computers and technology as an open source contributor and product manager

View project on GitHub

Notes on RESTful API Design

This is a list of return codes in relation to a RESTful API design.

Some of the operations can have more than one outcome, so you need to decide on which one you find the most appropriate for your use case.

</td> </table>
OperationHTTP MethodStatus Code On SuccessNotes
CreatePOST201 CreatedWhen the object is created immediately
202 AcceptedWhen the object is accepted but not created immediately</td</tr>
400 Bad RequestIf the submitted data are malformed
404 Not FoundIf referenced objects do not exist
409 ConflictIf you handle that the same object cannot be created more than once
422 Unprocessable ContentIf submitted data are validated and validation fails
ReadGET200 OKWhen the object requested in included in the response
404 Not FoundIf referenced object/objects do not exist
UpdatePUT200 OKWhen the updated object is returned as part of the response
204 No ContentWhen the updated object is not returned as part of the response
400 Bad requestIf the submitted data are malformed
404 Not FoundIf referenced object/objects do not exist
409 ConflictIf you handle that the object cannot be updated inconsistently
PATCH200 OKWhen the updated object is returned as part of the response
204 No ContentWhen the updated object is not returned as part of the response
400 Bad RequestIf the submitted data are malformed
404 Not FoundIf referenced object/objects do not exist
DeleteDELETE200 OKWhen an object changes status to deleted or similar (soft delete) and is returned as part of the response
202 AcceptedWhen an object changes status to deleted or similar and is deleted a part of a garbage collection process or similar
204 No ContentWhen an object is deleted immediately and the object is not returned
404 Not FoundIf referenced object do not exist
Status Code On ErrorNotes
401 UnauthorizedIf you receive a unauthenticated request, to a resource requiring authentication
403 ForbiddenIf you receive a authenticated but unauthorized request, to a resource requiring authorization
405 Method not allowedIf you receive a HTTP method not supported
429 Too Many RequestsIf you support rate limiting and set limit is reached
500 Internal Server ErrorFor you unhandled errors and errors server side
HTTP MethodIdempotentCan become Idempotent
POSTNoYes
PUTYes
PATCHNo
GETYes
HEADYes
DELETEYes
OPTIONSYes
## Resources and References - [MDN: 200 OK](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/200) - [MDN: 201 Created](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/201) - [MDN: 202 Accepted](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/202) - [MDN: 204 No Content](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/204) - [MDN: 400 Bad Request](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/400) - [MDN: 401 Unauthorized](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/401) - [MDN: 403 Forbidden](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/403) - [MDN: 404 Not Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/404) - [MDN: 405 Method not allowed](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/405) - [MDN: 409 Conflict](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/409) - [MDN: 429 Too many requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/429) - [MDN: 500 Server Error](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/500) - [MDN: 502 Bad Gateway](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/502) - [MDN: 503 Service Unavailable](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/503) - [MDN: 504 Gateway Timeout](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/504)