HTTP Methods

  • 🔗 restfulapi.net - 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 MethodCRUDCollection Resource (e.g. /users)Single Resouce (e.g. /users/123)
GETRead200 (OK), list of users. Use pagination, sorting, and filtering to navigate big lists200 (OK), single user. 404 (Not Found), if ID not found or invalid
POSTCreate201 (Created), ‘Location’ header with link to /users/{id} containing new IDAvoid using POST on a single resource
PUTUpdate/Replace405 (Method not allowed), unless you want to update every resource in the entire collection of resource200 (OK) or 204 (No Content). Use 404 (Not Found), if ID is not found or invalid
PATCHPartial Update/Modify405 (Method not allowed), unless you want to modify the collection itself200 (OK) or 204 (No Content). Use 404 (Not Found), if ID is not found or invalid
DELETEDelete405 (Method not allowed), unless you want to delete the whole collection — use with caution200 (OK). 404 (Not Found), if ID not found or invalid

Status Code

(tabella generata da ChatGPT 3.5)

Status CodeVerbose Description
100 ContinueThe server has received the request headers, and the client should proceed to send the request body.
101 Switching ProtocolsThe requester has asked the server to switch protocols, and the server has agreed to do so.
200 OKThe request was successful, and the server has returned the requested data.
201 CreatedThe request was successful, and a new resource has been created as a result.
202 AcceptedThe request has been accepted for processing, but the processing has not been completed yet.
203 Non-Authoritative InformationThe server is returning non-authoritative information since it is not the original source of data.
204 No ContentThe request was successful, but there is no response body to return.
205 Reset ContentThe request was successful, and the user agent should reset the document view.
206 Partial ContentThe server is delivering only part of the resource because the client requested a specific range of bytes.
300 Multiple ChoicesThe request has multiple possible responses, and the client should choose one.
301 Moved PermanentlyThe requested resource has been permanently moved to a new URL.
302 FoundThe requested resource has been temporarily moved to a different URL.
303 See OtherThe response to the request can be found under a different URI.
304 Not ModifiedThe resource has not been modified since the last request.
305 Use ProxyThe client must use a proxy server to access the requested resource.
307 Temporary RedirectThe requested resource is temporarily available under a different URI.
308 Permanent RedirectThe requested resource has been permanently moved to a different URI.
400 Bad RequestThe request is malformed or contains bad syntax.
401 UnauthorizedAuthentication is required, and the provided credentials are invalid.
402 Payment RequiredPayment is required to access the requested resource.
403 ForbiddenThe client does not have permission to access the requested resource.
404 Not FoundThe requested resource could not be found on the server.
405 Method Not AllowedThe requested method is not allowed for the specified resource.
406 Not AcceptableThe requested resource cannot produce the desired response format.
407 Proxy Authentication RequiredAuthentication is required to access the proxy.
408 Request TimeoutThe server terminated the request due to an excessive timeout.
409 ConflictThe request could not be completed due to a conflict with the current state of the resource.
410 GoneThe requested resource is no longer available at the server.
411 Length RequiredThe server requires a Content-Length header in the request.
412 Precondition FailedOne or more conditions specified in the request header fields evaluated to false.
413 Payload Too LargeThe request is larger than the server is willing or able to process.
414 URI Too LongThe requested URI is longer than the server is willing to process.
415 Unsupported Media TypeThe request entity has a media type that the server does not support.
416 Range Not SatisfiableThe requested range cannot be satisfied by the server.
417 Expectation FailedThe server cannot meet the requirements specified in the Expect header field.
418 I’m a teapotI’m a teapot. This is not a serious HTTP status code.
422 Unprocessable EntityThe request was well-formed but semantically incorrect.
423 LockedThe resource is locked and cannot be accessed.
424 Failed DependencyThe request failed because it depended on another request that failed.
426 Upgrade RequiredThe server requires the request to be upgraded to a different protocol.
428 Precondition RequiredThe server requires the request to be conditional.
429 Too Many RequestsThe user has sent too many requests in a given amount of time.
431 Request Header Fields Too LargeThe server is unwilling to process the request because the request header fields are too large.
451 Unavailable For Legal ReasonsThe server is denying access to the resource due to legal reasons.
500 Internal Server ErrorAn unexpected condition prevented the server from fulfilling the request.
501 Not ImplementedThe server does not support the functionality required to fulfill the request.
502 Bad GatewayThe server, while acting as a gateway or proxy, received an invalid response from the upstream server.
503 Service UnavailableThe server is currently unable to handle the request due to temporary overloading or maintenance of the server.
504 Gateway TimeoutThe server, while acting as a gateway or proxy, did not receive a timely response from the upstream server.
505 HTTP Version Not SupportedThe server does not support the HTTP protocol version used in the request.
506 Variant Also NegotiatesTransparent content negotiation for the request results in a circular reference.
507 Insufficient StorageThe server is unable to store the representation needed to complete the request.
508 Loop DetectedThe server detected an infinite loop while processing the request.
510 Not ExtendedFurther extensions to the request are required for the server to fulfill it.
511 Network Authentication RequiredThe 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.