Files
live-campus-mcs-p-2027.2/Semaine_11/Jour_01/api_inventaire.md
T
gauvainboiche 0d071d2843 Semaine 11
2026-07-11 21:38:00 +02:00

3.2 KiB

Exercice 1.3 "API REST" - Gauvain BOICHÉ

Commandes lancées

1. Liste vide

curl -i http://127.0.0.1:8000/equipments curl -i http://127.0.0.1:8000/equipments/1

2. Création

curl -i -X POST http://127.0.0.1:8000/equipments
-H "Content-Type: application/json"
-d '{"hostname":"srv-dns-01","ip":"192.168.1.20"}'

3. Lecture

curl -i http://127.0.0.1:8000/equipments/1

4. Erreurs

curl -i http://127.0.0.1:8000/equipments/abc curl -i http://127.0.0.1:8000/equipments/99 curl -i -X POST http://127.0.0.1:8000/equipments
-H "Content-Type: application/json" -d '{"ip":"1.1.1.1"}'

Résultats

curl -i -X POST http://127.0.0.1:8000/equipments -H "Content-Type: application/json" -d '{"hostname":"srv-dns-01","ip":"192.168.1.20"}'

1. Liste vide

Côté client

> curl -i http://127.0.0.1:8000/equipments
HTTP/1.0 200 OK
Server: BaseHTTP/0.6 Python/3.12.12
Date: Mon, 06 Jul 2026 14:21:49 GMT
Content-Type: application/json
Content-Length: 2

[]

> curl -i http://127.0.0.1:8000/equipments/1
HTTP/1.0 404 Not Found
Server: BaseHTTP/0.6 Python/3.12.12
Date: Mon, 06 Jul 2026 14:26:13 GMT
Content-Type: application/json
Content-Length: 41

{"erreur": "\u00e9quipement introuvable"}

Côté serveur

127.0.0.1 - - [06/Jul/2026 16:21:49] "GET /equipments HTTP/1.1" 200 -
127.0.0.1 - - [06/Jul/2026 16:24:39] "GET /equipments/1 HTTP/1.1" 404 -

2. Création

Côté client

> curl -i -X POST http://127.0.0.1:8000/equipments -H "Content-Type: application/json" -d '{"hostname":"srv-dns-01","ip":"192.168.1.20"}'
HTTP/1.0 201 Created
Server: BaseHTTP/0.6 Python/3.12.12
Date: Mon, 06 Jul 2026 14:26:31 GMT
Content-Type: application/json
Content-Length: 57

{"id": 1, "hostname": "srv-dns-01", "ip": "192.168.1.20"}

Côté serveur

127.0.0.1 - - [06/Jul/2026 16:26:31] "POST /equipments HTTP/1.1" 201 -

3. Lecture

Côté client

> curl -i http://127.0.0.1:8000/equipments/1
HTTP/1.0 200 OK
Server: BaseHTTP/0.6 Python/3.12.12
Date: Mon, 06 Jul 2026 14:26:45 GMT
Content-Type: application/json
Content-Length: 57

{"id": 1, "hostname": "srv-dns-01", "ip": "192.168.1.20"}

Côté serveur

127.0.0.1 - - [06/Jul/2026 16:26:45] "GET /equipments/1 HTTP/1.1" 200 -

4. Erreurs

Côté client

> curl -i http://127.0.0.1:8000/equipments/abc
HTTP/1.0 400 Bad Request
Server: BaseHTTP/0.6 Python/3.12.12
Date: Mon, 06 Jul 2026 14:27:32 GMT
Content-Type: application/json
Content-Length: 27

{"erreur": "id non entier"}

> curl -i http://127.0.0.1:8000/equipments/99
HTTP/1.0 404 Not Found
Server: BaseHTTP/0.6 Python/3.12.12
Date: Mon, 06 Jul 2026 14:30:26 GMT
Content-Type: application/json
Content-Length: 41

{"erreur": "\u00e9quipement introuvable"}

> curl -i -X POST http://127.0.0.1:8000/equipments -H "Content-Type: application/json" -d '{"ip":"1.1.1.1"}'
HTTP/1.0 400 Bad Request
Server: BaseHTTP/0.6 Python/3.12.12
Date: Mon, 06 Jul 2026 14:30:48 GMT
Content-Type: application/json
Content-Length: 35

{"erreur": "hostname et ip requis"}

Côté serveur

127.0.0.1 - - [06/Jul/2026 16:27:32] "GET /equipments/abc HTTP/1.1" 400 -
127.0.0.1 - - [06/Jul/2026 16:30:26] "GET /equipments/99 HTTP/1.1" 404 -
127.0.0.1 - - [06/Jul/2026 16:30:48] "POST /equipments HTTP/1.1" 400 -