HTTP methods – REST APIs

HTTP methods, also known as verbs, define the type of action a client can perform on a resource in a RESTful API. Each method represents a specific operation that defines the endpoint’s intent on a resource. Here is a list of the most frequently used methods, what they are for, and their expected success status code:

MethodTypical roleSuccess status code
GETRetrieve a resource (read data).200 OK
POSTCreate a new resource.201 CREATED
PUTReplace a resource.200 OK or 204 No Content
DELETEDelete a resource.200 OK or 204 No Content
PATCHPartially update a resource.200 OK

Next, we explore the commonly used status codes.

HTTP Status code

HTTP status codes are part of the HTTP response and provide the client with information about the success or failure of their request; the status of the request.Status codes touching similar subjects are grouped under the same broad “hundredth” categories:

The following table explains some of the most common ones:

Status codeRole
200 OKIndicates the request has succeeded. It usually includes data related to the resource in the response body.
201 CREATEDIndicates the has succeeded and the system created a resource. It should also include a Location HTTP header pointing to the newly created resource and can include the new entity in the response body.
202 ACCEPTEDIndicates the request has been accepted for processing but is not processed yet. We use this code for asynchronous operations. In an event-driven system (see Chapter 17, Introduction to Microservices Architecture), this could mean that an event has been published, the current resource has completed its job (published the event), but to know more, the client needs to contact another resource, wait for a notification, just wait, or can’t know.
204 NO CONTENTIndicates the request has succeeded with no content in the response body.
302 FOUNDIndicates that the requested resource resides temporarily under a different URL specified in the Location header. We commonly use this status code for redirection.
400 BAD REQUESTIndicates that the server could not understand or process the request. This usually relates to a validation error like a bad input or a missing field.
401 UNAUTHORIZEDIndicates that the request requires user authentication to access the resource.
403 FORBIDDENIndicates that the server understood the request but refused to authorize it. This usually means the client lacks the access rights for the resource (authorization).
404 NOT FOUNDIndicates the resource does not exist or was not found. REST APIs often return this from valid endpoints.
409 CONFLICTIndicates that the server cannot complete the request due to a conflict with the current state of the resource. A typical scenario would be that the entity has changed between its read operation ( GET ) and the current update ( PUT ) operation.
500 INTERNAL SERVER ERRORIndicates that an unhandled error occurred on the server side and prevented it from fulfilling the request.

Now that we covered the HTTP methods and status codes, we look at how to pass more metadata between the client and the server.

You may also like