How do I make an API request in HTTP?

To use an application with an API available on a server, a request is made to the API. This request is formulated in HTTP, and contains the information required to ensure that the response sent back corresponds to the request.

The request is structured in four parts: method, route, header and body.

The HTTP protocol 

The Hypertext Transfer Protocol (HTTP ) is the foundation of communication on the World Wide Web. It is a protocol based on the client-server model, enabling the exchange of resources between a client (usually a web browser) and a web server.

Communication via HTTP is initiated by the client, who submits an HTTP request to the server. After processing the request, the server returns an HTTP response to the client.

The HTTP protocol has two distinctive features: 

Firstly, it isstateless, meaning that each request is processed independently of the others. This means that no information about previous requests is retained. What's more, the protocol is also text-based: HTTP requests and responses are mainly composed of text, making them easy to inspect and debug.

How do you formulate your request? 

An HTTP request is structured into several components:

The HTTP Method

The method indicates the type of request, i.e. the action the client wants the server to perform on the identified resource. Here are a few examples: 

  • GET: Retrieve a resource 
  • POST: Create a new resource
  • PUT: partially modify a resource
  • DELETE: delete a resource

The road

The route specifies the path to take to access the target resource on the server.

For example, if the base URL of a book management API is https://api.example.com/v1/, the routes could be :

books: to access the list of all books.

books/123 : to access the book whose identifier is 123.

authors/456/books: to access the list of books by the author whose identifier is 456.

By combining the HTTP method and the route, we can precisely define the operation requested and the resource on which it is to be applied. 

The header 

The HTTP header provides additional information about the request itself or about the client. It can be used to add context or instructions for the server. Here are some examples of information commonly found in headers :

  • Authentication: Authentication data, such as tokens or IDs and passwords, are often included in headers to enable the client to access authentication-protected resources.
  • Cookie management: Since the HTTP protocol is stateless, cookies are used to maintain session status between requests. They are therefore transmitted in the
  • Expected response format: The client can specify its preferred response format using the Accept header. For example, Accept: application/json indicates that the client prefers to receive the response in JSON format.
  • Request content type: When the request contains a body (for example, with POST or PUT methods), the Content-Type header indicates the format of the data sent (for example, Content-Type: application/json if the data is in JSON format, Content-Type: application/x-www-form-urlencoded for form data).
  • Client information: The User-Agent header provides information about the browser or client application making the request.

In short, headers are essential for configuring the request and ensuring that the server understands the client's context and expectations.

The body

The body contains all the information related to the context of the request. 

For example, if a customer wishes to create a new user, the POST request body to a route such as /users could contain the user's information in JSON format:

JSON

{

    "username": "new_user",

    "email": "nouvel_utilisateur@exemple.com",

    "password": "motdepasse

}

H2 HTTP server responses

When the request is sent, the API responds with HTTP codes. For example, code 404 indicates that the resource does not exist. Some responses also contain a body

Here, the customer requests a list of items, so the response contains a body with the list of requested items, in JSON format.

Are you interested in this topic?

CONTACT US