Short Posts, Web Development

HTTP Request Methods For Beginners

As Software Engineers, at some point in our careers we are either going to be creating or consuming external APIs. In this post, I broke down the common HTTP Request Methods that exist today.

What is HTTP?

HTTP (full name Hypertext 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 you to read. See below:

HTTP Request-Response Overview
HTTP Request-Response Overview

Now that we’ve been introduced to HTTP, let’s take a look at HTTP Methods.

HTTP Methods

When making HTTP requests, the client has to specify the particular action to be performed on a given resource. HTTP has a set of request methods for this purpose. Let’s meet them below:

 

GET

You 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.

POST

You can use the POST request to submit an entity to the specified resource. POST requests are commonly used when trying to create a nw 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.

PUT

You 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 (e.g. change 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.

PATCH

A PATCH request can be used to partially update or modify a resource. Unlike PUT, PATCH requests are not safe or idempotent.

An instance when PATCH can be used is when updating just one field in a record. In this case, since the entire record shouldn’t be updated because sending the full payload requires unnecessary bandwidth a PATCH request could suffice.

DELETE

A DELETE request can be used to delete a specified resource. Calling a delete on the endpoint v1.0/orders/{id} will delete an order having that particular identifier if the order exists.

 

CONCLUSION

Other HTTP request methods such as CONNECT, OPTIONS and TRACE also exist to read more about them, click this link.

This article was created from the original Instagram post I made about HTTP Request Methods. Did you find this article useful? You can buy me a coffee or share this on twitter. You can also say hello if you have more questions about HTTP Request Methods ❤️.

 

 

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *