HTTP Requests

When we submit a form, the data is sent to the server through an HTTP request. But what exactly is an HTTP request?

HTTP Requests

HTTP (Hypertext Transfer Protocol) is a protocol that allows communication between a client (for example, a web browser) and a server.

Good to know

An HTTP request is a message sent by the client to the server to ask it to perform an action (like retrieving a web page or sending data).

It's important to note that each type of HTTP request has a particular intention, but in practice, nothing prevents a developer from misusing them. For example, the PUT method is generally used to update a resource, but a developer could very well use it to delete data if they wanted. This is a matter of convention and best practices. Using HTTP methods unconventionally can make code difficult to maintain and understand by other developers, but technically, the protocol doesn't prevent it.

There are several types of HTTP requests, but we will focus on the most common ones:

GET

The GET method is used to request a resource from the server, for example a web page. Data can be sent in the URL as parameters (after a ?).

Example:

GET /search?q=html HTTP/1.1
Host: www.example.com

This example asks the server to search for information about "html".

POST

The POST method is used to send data to the server, for example when you submit a registration form.

Example:

POST /submit HTTP/1.1
Host: www.example.com
Content-Type: application/x-www-form-urlencoded

username=alice&password=12345

In this example, form data is sent to the server in the request body.

PUT

The PUT method is used to send data to the server, often to create or update a resource. It's similar to POST, but PUT is often used for idempotent operations (meaning the same request will have the same effect, even if repeated).

Example:

PUT /user/123 HTTP/1.1
Host: www.example.com
Content-Type: application/json

{
  "username": "alice",
  "email": "[email protected]"
}

This example updates the information of the user with ID 123.

DELETE

The DELETE method is used to delete a resource on the server.

Example:

DELETE /user/123 HTTP/1.1
Host: www.example.com

This example asks the server to delete the user with ID 123.