Skip to content

Exemples de messages HTTP

Cette section fournit des exemples concrets d’échanges de requêtes et de réponses HTTP, illustrant des scénarios courants et l’utilisation de diverses méthodes et en-têtes.

Exemple 1 : Requête GET pour une page HTML

Section titled “Exemple 1 : Requête GET pour une page HTML”

Le client demande la page d’accueil de www.example.com.

GET / HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8
Accept-Language: en-US,en;q=0.9
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Upgrade-Insecure-Requests: 1
HTTP/1.1 200 OK
Date: Wed, 23 Feb 2022 10:00:00 GMT
Server: MyCustomServer/1.0
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Content-Length: 612 (length of the gzipped content)
Last-Modified: Tue, 22 Feb 2022 15:30:00 GMT
ETag: "abcdef123456"
Cache-Control: public, max-age=3600
Connection: keep-alive
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example Domain</title>
</head>
<body>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
</body>
</html>
(Le contenu réel serait compressé avec gzip en transit)

Exemple 2 : Requête POST avec données JSON vers une API

Section titled “Exemple 2 : Requête POST avec données JSON vers une API”

Le client envoie des données JSON pour créer une nouvelle ressource à un point d’API.

POST /api/items HTTP/1.1
Host: api.example.com
User-Agent: MyApiClient/2.1
Content-Type: application/json; charset=utf-8
Content-Length: 35
Accept: application/json
Authorization: Bearer <your_jwt_token_here>
Connection: keep-alive
{
"name": "New Item",
"price": 19.99
}
HTTP/1.1 201 Created
Date: Wed, 23 Feb 2022 10:05:00 GMT
Server: Nginx/1.20.1
Content-Type: application/json; charset=utf-8
Content-Length: 58
Location: /api/items/123
Connection: keep-alive
{
"id": 123,
"name": "New Item",
"price": 19.99
}

Exemple 3 : Requête GET pour une ressource introuvable

Section titled “Exemple 3 : Requête GET pour une ressource introuvable”

Le client demande une page inexistante.

GET /nonexistentpage.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0
Accept: text/html,*/*
Connection: keep-alive
HTTP/1.1 404 Not Found
Date: Wed, 23 Feb 2022 10:10:00 GMT
Server: Apache/2.4.52 (Ubuntu)
Content-Type: text/html; charset=iso-8859-1
Content-Length: 289
Connection: close
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /nonexistentpage.html was not found on this server.</p>
<hr>
<address>Apache/2.4.52 (Ubuntu) Server at www.example.com Port 80</address>
</body></html>

Exemple 4 : Requête GET conditionnelle utilisant ETag

Section titled “Exemple 4 : Requête GET conditionnelle utilisant ETag”

Le client demande une ressource qu’il a mise en cache, en fournissant l’ETag pour vérifier les modifications.

GET /styles/main.css HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0
Accept: text/css,*/*;q=0.1
If-None-Match: "xyz789abc"
Connection: keep-alive

Réponse du serveur (304 Not Modified, si l’ETag correspond) :

Section titled “Réponse du serveur (304 Not Modified, si l’ETag correspond) :”
HTTP/1.1 304 Not Modified
Date: Wed, 23 Feb 2022 10:15:00 GMT
Server: MyCustomServer/1.0
ETag: "xyz789abc"
Cache-Control: public, max-age=86400
Connection: keep-alive
(Pas de corps de message)

Exemple 5 : Requête PUT pour mettre à jour une ressource

Section titled “Exemple 5 : Requête PUT pour mettre à jour une ressource”

Le client met à jour une ressource existante en fournissant sa nouvelle représentation complète.

PUT /api/items/123 HTTP/1.1
Host: api.example.com
User-Agent: MyApiClient/2.1
Content-Type: application/json; charset=utf-8
Content-Length: 48
Accept: application/json
Authorization: Bearer <your_jwt_token_here>
If-Match: "oldETagValue"
Connection: keep-alive
{
"name": "Updated Item Name",
"price": 24.99
}

Réponse du serveur (mise à jour réussie) :

Section titled “Réponse du serveur (mise à jour réussie) :”
HTTP/1.1 200 OK
Date: Wed, 23 Feb 2022 10:20:00 GMT
Server: Nginx/1.20.1
Content-Type: application/json; charset=utf-8
Content-Length: 75
ETag: "newETagValue"
Connection: keep-alive
{
"id": 123,
"name": "Updated Item Name",
"price": 24.99,
"updatedAt": "2022-02-23T10:20:00Z"
}
Section titled “Exemple 6 : Réponse du serveur avec plusieurs en-têtes Set-Cookie”

Le serveur définit deux cookies pour le client.

Réponse du serveur (faisant partie d’une réponse plus large, par exemple, après connexion) :

Section titled “Réponse du serveur (faisant partie d’une réponse plus large, par exemple, après connexion) :”
HTTP/1.1 200 OK
Date: Wed, 23 Feb 2022 10:25:00 GMT
Server: MyAuthServer/1.0
Content-Type: application/json
Set-Cookie: sessionToken=a1b2c3d4e5f6; Path=/; Secure; HttpOnly; SameSite=Lax
Set-Cookie: userPrefs=theme=dark&lang=en; Path=/; Max-Age=31536000; Secure; SameSite=Lax
Connection: keep-alive
{
"status": "Login successful",
"user_id": "user789"
}