All about HTTP Requests and Response

Kailash Kejriwal
5 min readOct 5, 2021

--

In my last article, I ended with a note that we will design routes to handle requests from clients and send a response. Before proceeding let me brief you about these terms.

What are Request and Response?

So what is a ‘Request’? In our daily life, we often make requests to someone. We may request for some resources like books, money or anything else. Sometimes we may make a request for services like validation, or get some task done. Similar to that, we make ‘requests’ to the server for files, data, and other services.

And corresponding to each request, we get a ‘response’ back from the server which brings us the data we asked for, or tells us the status whether the task was successfully completed or not.

A Request-Response System

Making a Request

Now, how to make a request? Application Program Interface or API is the intermediary that you use to make a request. Let us take an example. Consider the URL

https://jsonplaceholder.typicode.com/todos

This URL brings us random tasks in a todo list. Now lets understand how it is working. The part https://jsonplaceholder.typicode.com is the domain. It is where all the requests will be made. The remaining part /posts is the path or route of API. It generally tells the server what kind of data you want and how you want it. The server then follows the route and prepares the data to be served to you. Finally it is sent back to the client in the form of a response; posts as shown in the example.

To test our APIs, we generally use a tool called Postman. It helps us test our API endpoints. You can try it from here.

Let’s test the URL in the example.

Example of a Request

Here you can see that we have our data in JSON format (the general format to exchange data between client and server). In the response, all the objects are clubbed together into an array where each of the objects represent a post. Each post again has 4 properties or ‘fields’ namely userId, id, title and body.

Queries and Parameters

Now, what if we want to fetch a single post based on the id of the task. Parameters and queries come into the picture here. Queries are set within the route, while parameters are sent as separate variables inside the route. Each query or parameter represents the field based on which the response data will be filtered.

Suppose we want to bring the post having id of 2.

Using query,

Fetching filtered data using query

Using parameters,

Fetching filtered data using parameter

So you can see that the response in both the cases is the same, although the request URL is different. You can apply multiple parameters in a request URL by appending the key-value pair with the ‘&’ character.

Request Headers

Before moving on, let me brief you about headers. Request Header is an object that stores the information of the request, like the content-type within the request body, any authorization field, etc. Similarly, response headers store information about the response. A response header would look like

Request Methods

In all the above examples we observe that the request method is set to GET. You may ask what is a ‘request method’? Well,Request Methods define the primary action that we are asking the server to perform. There are generally 5 types of request methods:

  • GET: As already seen above, this method simply ‘gets’ or brings us the data we want. This is the most simple one of all. If you want to filter the data based on some specific properties, you can add queries or parameters as shown before.
  • POST: Whenever you make a GET request, the parameters get exposed in the request URL. Now, if it is some sensitive data like a password, it may get exposed to someone. So we need to hide the data we are sending. Here, the POST request comes into play. For a post request, we define a body field which contains the data we want to send and the body remains non-exposed throughout the session. We can also send media files within the body, which is not possible for a GET request.

Let us test a POST request with userId = 1.

So, a new post is created with an id of 101.

  • PUT: It is used to update the current data in the database, or add new new data into the database. Let us modify the body of the blog with id of 1.

So, the body now has the modified data that we have inserted.

  • PATCH: This method is similar to put except that it gives the server the instructions on how to update or modify the data. Let us patch the title of the blog with id of 2.

The data ,here too, gets updated.

  • DELETE: As the name suggests, it is used to perform a delete operation, and remove data from the database.

Let us delete the post with id of 2

Now that you know about request and its types, response, queries, parameters, headers, you can easily customize request URLs and fetch data as per your need.

In my next blog, I will explain about handling of client requests by a server.

Thank You. Keep Coding !

--

--