In this blogpost we’ll explore the parts of a HTTP Request and a HTTP Response.
HTTP Request
When we work with Web API’s, the communication between our clients and our API will be done using HTTP requests. An HTTP request is a message that one computer sends to another using the HTTP protocol. The HTTP request is made by the clients of our API to our API. When our API receives this request, it processes it, and then returns a response, called an HTTP response. The clients and our API will communicate using HTTP requests and responses. These messages have a structure, and in this section we will study that structure.
In general, an HTTP request is divided into 3 parts:
- A request line
- A set of header fields
- A body, which is optional
Let’s see these parts.
Request Line
In the request line we place the HTTP method to be used, the URI of the request and the HTTP protocol to be used. That is, they have the following structure:
HTTP-METHOD URI HTTP-PROTOCOL
We will talk about HTTP methods in the next post, but basically HTTP methods indicate what kind of action our client wants to perform, whether he wants to read a resource, or if he wants to send information to the API, etc. The URI refers to the address where the resource is located. And the HTTP protocol refers to which HTTP protocol will be used, this is because there are several versions of the HTTP protocol, at the time of writing this post, the most common protocol is HTTP/1.1, however, there are other more recent revisions , like the HTTP/2.0 revision.
Let’s see an example of a request line:
GET /api/authors HTTP/1.1
We see that the HTTP method is GET, which means that we want to read a resource, which resource? Well the one indicated afterward, in /api/authors. Finally, it is indicated the HTTP protocol to be used. Then, this request line tells our API that a user or client wants to read the resource found in the URI /api /authors.
Let’s see another example:
POST /test.html HTTP/1.1
Here the HTTP Method is POST, which means that the user wants to send us something. That something the user wants to send us will be found in the body of the request, we will see this in a few minutes. We see that the URI is /test.html, and the procolo is HTTP/1.1.
The second part of an HTTP request is the header.
Request Header
The header of the request is where the headers of the request are located. Headers are metadata that are sent in the request to provide information about the request. Each header is specified with a name, then two points, and then followed by the value of that header.
Let’s see an example of a header:
Host: en.wikipedia.org
In this case, the name of the header is Host, and its value is en.wikipedia.org. The Host header indicates the domain of the server.
Another header example could be:
Cache-Control: no-cache
The name of this header is Cache-Control, and its value is no-cache.
There may be multiple headers in the header of the request. Let’s see an example of a petition with its request line and header:
GET /api/autores HTTP/1.1 Host: en.wikipedia.org Cache-Control: no-cache
Here we see that the first line is the request line, below this is the header of the request, which is composed of several individual headers, host and cache-control in our case.
The Host and Cache-Control headers are standard headers, which already have a well-defined purpose. However, we are free to use our own custom headers. When we need to express our metadata of our request, we can use custom headers. All you need to do is send them in the header of the HTTP request.
Let’s now look at the third and final part of an HTTP request, the body.
Request Body
The Request Body is where we put additional information that we are going to send to the server. In the body of the request we are free to place virtually whatever we want. From the username and password of a person trying to login to our system, to the answers of a complex form of a survey. The body is quite important, because it represents, in many cases, the content per se that one wants to transmit.
We note that GET requests do not use a body, because one does not tend to send many complex data when reading information. In the case of the POST method, we usually use the body of the request to place what we want to send.
Let’s see an example of a request body:
Hello
Here we have a simple “Hello”. The body of the request is to send virtually anything we want, from a simple greeting, to a little more structured information, such as the following:
{ "Name": "Felipe Gavilán", "Age": 999 }
Here we have data in JSON format that represents the body of our request.
Let’s see then an HTTP request with its three parts:
POST /api/authors HTTP/1.1 Host: myWebApi.com Content-Type: application/json Cache-Control: no-cache { "Name": "Felipe Gavilán", "Age": 999 }
The request we are looking at is a POST towards the resource /api /authors. We have a header composed of 3 headers: Host, Content-Type and Cache-Control. And finally, at the bottom we have the body of the petition. Notice that a blank line is the separation between the HTTP header and the body.
HTTP Response
As we said at the beginning of this entry, when the client sends us an HTTP request, our server must respond with an HTTP response. The HTTP response also has its own structure, which is quite similar to the structure of the request. These parts are:
- Status line
- Header
- Body, optional
The request status is indicated in the status line, that is, if it was successful, if there was an error, or if it is required that we take some type of action. The HTTP status codes is something we will see in a future entry. The header is a set of headers, just like the request header. The server can send us as many headers as it wants. And, finally, the server has the option to send a body with the data that it wants to transmit to us. Although the body is also optional, it is quite fundamental when it comes to using web pages, since it is through the body that we receive the HTML of a web page that we want to have displayed in our web browser.
Example of an HTTP response:
HTTP/1.1 200 OK Date: Thu, 03 Jan 2019 23:26:07 GMT Server: gws Accept-Ranges: bytes Content-Length: 68894 Content-Type: text/html; charset=UTF-8 <!doctype html><html …
In the first line we have the status line, the answer was a 200 OK. Then we have the headers of the answer. Finally, separated by a blank line from the headers, we have the body of the response, which in this case is an HTML document.
Summary
An HTTP request is divided into three parts: Request line, header and body. An HTTP response is also divided into three parts: Status line, header and body.