HTTP Methods
-
GET: retrieve resources representation/information only. GET is a safe method because it doesn’t change the resource’s state.
- GET should never be used to create or update resources because URI have a limited amount of characters
-
POST: creare new subordinate resources, e.g a file is subordinate to a directory containing it or a row is subordinate to a database table
- POST method pass to the API the Request Body, allowing to treat received data as objects or JSON
Lesser used methods:
- PUT: update an existing resource - if the resource doesn’t exists the API may decide to create it or not
- DELETE: delete the resources identified by the URI
- PATCH: to make a partial update
| HTTP Method | CRUD | Collection Resource (e.g. /users) | Single Resouce (e.g. /users/123) |
|---|---|---|---|
| GET | Read | 200 (OK), list of users. Use pagination, sorting, and filtering to navigate big lists | 200 (OK), single user. 404 (Not Found), if ID not found or invalid |
| POST | Create | 201 (Created), ‘Location’ header with link to /users/{id} containing new ID | Avoid using POST on a single resource |
| PUT | Update/Replace | 405 (Method not allowed), unless you want to update every resource in the entire collection of resource | 200 (OK) or 204 (No Content). Use 404 (Not Found), if ID is not found or invalid |
| PATCH | Partial Update/Modify | 405 (Method not allowed), unless you want to modify the collection itself | 200 (OK) or 204 (No Content). Use 404 (Not Found), if ID is not found or invalid |
| DELETE | Delete | 405 (Method not allowed), unless you want to delete the whole collection — use with caution | 200 (OK). 404 (Not Found), if ID not found or invalid |
Status Code
(tabella generata da ChatGPT 3.5)
| Status Code | Verbose Description |
|---|---|
| 100 Continue | The server has received the request headers, and the client should proceed to send the request body. |
| 101 Switching Protocols | The requester has asked the server to switch protocols, and the server has agreed to do so. |
| 200 OK | The request was successful, and the server has returned the requested data. |
| 201 Created | The request was successful, and a new resource has been created as a result. |
| 202 Accepted | The request has been accepted for processing, but the processing has not been completed yet. |
| 203 Non-Authoritative Information | The server is returning non-authoritative information since it is not the original source of data. |
| 204 No Content | The request was successful, but there is no response body to return. |
| 205 Reset Content | The request was successful, and the user agent should reset the document view. |
| 206 Partial Content | The server is delivering only part of the resource because the client requested a specific range of bytes. |
| 300 Multiple Choices | The request has multiple possible responses, and the client should choose one. |
| 301 Moved Permanently | The requested resource has been permanently moved to a new URL. |
| 302 Found | The requested resource has been temporarily moved to a different URL. |
| 303 See Other | The response to the request can be found under a different URI. |
| 304 Not Modified | The resource has not been modified since the last request. |
| 305 Use Proxy | The client must use a proxy server to access the requested resource. |
| 307 Temporary Redirect | The requested resource is temporarily available under a different URI. |
| 308 Permanent Redirect | The requested resource has been permanently moved to a different URI. |
| 400 Bad Request | The request is malformed or contains bad syntax. |
| 401 Unauthorized | Authentication is required, and the provided credentials are invalid. |
| 402 Payment Required | Payment is required to access the requested resource. |
| 403 Forbidden | The client does not have permission to access the requested resource. |
| 404 Not Found | The requested resource could not be found on the server. |
| 405 Method Not Allowed | The requested method is not allowed for the specified resource. |
| 406 Not Acceptable | The requested resource cannot produce the desired response format. |
| 407 Proxy Authentication Required | Authentication is required to access the proxy. |
| 408 Request Timeout | The server terminated the request due to an excessive timeout. |
| 409 Conflict | The request could not be completed due to a conflict with the current state of the resource. |
| 410 Gone | The requested resource is no longer available at the server. |
| 411 Length Required | The server requires a Content-Length header in the request. |
| 412 Precondition Failed | One or more conditions specified in the request header fields evaluated to false. |
| 413 Payload Too Large | The request is larger than the server is willing or able to process. |
| 414 URI Too Long | The requested URI is longer than the server is willing to process. |
| 415 Unsupported Media Type | The request entity has a media type that the server does not support. |
| 416 Range Not Satisfiable | The requested range cannot be satisfied by the server. |
| 417 Expectation Failed | The server cannot meet the requirements specified in the Expect header field. |
| 418 I’m a teapot | I’m a teapot. This is not a serious HTTP status code. |
| 422 Unprocessable Entity | The request was well-formed but semantically incorrect. |
| 423 Locked | The resource is locked and cannot be accessed. |
| 424 Failed Dependency | The request failed because it depended on another request that failed. |
| 426 Upgrade Required | The server requires the request to be upgraded to a different protocol. |
| 428 Precondition Required | The server requires the request to be conditional. |
| 429 Too Many Requests | The user has sent too many requests in a given amount of time. |
| 431 Request Header Fields Too Large | The server is unwilling to process the request because the request header fields are too large. |
| 451 Unavailable For Legal Reasons | The server is denying access to the resource due to legal reasons. |
| 500 Internal Server Error | An unexpected condition prevented the server from fulfilling the request. |
| 501 Not Implemented | The server does not support the functionality required to fulfill the request. |
| 502 Bad Gateway | The server, while acting as a gateway or proxy, received an invalid response from the upstream server. |
| 503 Service Unavailable | The server is currently unable to handle the request due to temporary overloading or maintenance of the server. |
| 504 Gateway Timeout | The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server. |
| 505 HTTP Version Not Supported | The server does not support the HTTP protocol version used in the request. |
| 506 Variant Also Negotiates | Transparent content negotiation for the request results in a circular reference. |
| 507 Insufficient Storage | The server is unable to store the representation needed to complete the request. |
| 508 Loop Detected | The server detected an infinite loop while processing the request. |
| 510 Not Extended | Further extensions to the request are required for the server to fulfill it. |
| 511 Network Authentication Required | The client needs to authenticate to gain network access. |
Headers
Custom Headers
Standars don’t officially recomends anymore using the “X-” prefix. But as some people points, using an header formatted like “X-MyHeader-Custom” make it easier to differentiate between a custom header and an official one.