At some point in their careers, software engineers are going to be creating or consuming external APIs. This shot breaks down the common HTTP request methods that exist today.
HTTP (Hyper Text Transfer Protocol), is a protocol designed for clients’ applications to communicate with servers. Usually, a client sends the request, and a server receives that, performs some action, and sends a response back to the client. The response usually contains status information about the request and could contain other additional information.
Other protocols exist today, but HTTP is used a lot on the web. Imagine visiting a URL identifying an HTML resource. A call will be made from the browser to the server to fetch that resource and render it on the browser for us to read. See below:
Now that we’ve been introduced to HTTP, let’s look at HTTP methods.
When making HTTP requests, the client has to specify the particular action on a given resource. HTTP has a set of request methods for this purpose. Let’s take a look at them.
We can use the GET request to retrieve data about a resource. It could be a single data or a collection of data.
Calling a GET on the endpoint v1.0/requests
would return a list of requests. Calling a GET on the endpoint v1.0/requests/{id}
would return a request resource with the specified ID.
An example of a scenario where a GET request would be used is when a client wants to fetch all pending orders belonging to a user from a backend server.
We can use the POST request to submit an entity to the specified resource. POST requests are commonly used when trying to create a new record. This usually changes the state of the server.
Calling a POST on the endpoint v1.0/orders
would create a new order entry on the backend.
An example of a scenario where a POST request would be used is when a user wants to register on a web application. They enter their details through a form, and their records get created on the backend server. The request to create that record is a POST request.
We can use the PUT request to update a particular record with the new payload. A scenario for using a PUT request is when we have an order record, and we would like to update the details of that particular order at once, for example, changing the order status from in-transit to delivered.
Calling a PUT on the endpoint v1.0/orders/{id}
with a valid payload will update the order record bearing that particular ID with the new payload.
A PATCH request can be used to update or modify a resource partially. Unlike PUT, PATCH requests are not safe or idempotent.
An instance when PATCH can be used is updating just one field in a record. In this case, a PATCH request would suffice as the entire record shouldn’t be updated because sending the full payload requires unnecessary bandwidth.
A DELETE v1.0/orders/{id}
request can be used to delete a specified resource. Calling delete on the endpoint will delete an order having that particular identifier, if the order exists.
Free Resources