Skip to lesson

Practice: HTTP requests and responses

A review of this module. Skip it if you feel confident; the next module does not need it.

Fix the mistakes

The slips that cost marks in this module. Spot each one, then check the fix.

Most HTTP bugs come from four slips: the wrong method, an unchecked status code, a wrong header, or a curl command the shell changed before it ran. Each has a quick check and a reliable fix.

Mistake 1: the wrong method for the action

Updating one field with the wrong method, then with the right one.

Example 1 · Mistake: PUT with one field; fix: PATCH

Terminal
curl -s -X PUT http://localhost:8080/api/v1/products/4 --json '{"stock":20}'curl -s -X PATCH http://localhost:8080/api/v1/products/4 --json '{"stock":20}'
Sample output
$ curl -s -X PUT http://localhost:8080/api/v1/products/4 --json '{"stock":20}'
{"type":"https://shoplite.example/problems/validation-failed","title":"Validation failed","status":400,"detail":"The request has 5 invalid fields.","instance":"/api/v1/products/4","errors":[{"field":"sku","message":"is required"},{"field":"name","message":"is required"},{"field":"categoryId","message":"is required"},{"field":"price","message":"is required"},{"field":"isActive","message":"is required"}]}
$ curl -s -X PATCH http://localhost:8080/api/v1/products/4 --json '{"stock":20}'
{"id":4,"sku":"LAP-IPS3","name":"Lenovo IdeaPad Slim 3","categoryId":3,"categoryName":"Laptops","price":42990.00,"stock":20,"isActive":true}

What this code does

  • The goal is to change only the laptop's stock. A PUT says "this is the whole product", so the API rejects a body with one field and lists every missing one.
  • PATCH means "change these fields": the stock becomes 20 and every other field stays as it was.

The method must match what the request does. A GET that changes data can be triggered by any prefetch or refresh, a PUT with one field is rejected or wipes the other fields, and a POST used to "update" hides the fact that retrying it is unsafe.

The check is one question: read, create, replace all, change part, or remove? The answer is GET, POST, PUT, PATCH or DELETE. Actions with rules, such as cancelling an order, are a POST to an action URL.

Mistake 2: not checking the status code

A missing product answered with -s alone, then with the status line.

Example 2 · Mistake: trusting the body without the status

Terminal
curl -s http://localhost:8080/api/v1/products/99curl -si http://localhost:8080/api/v1/products/99 | head -1
Sample output
$ curl -s http://localhost:8080/api/v1/products/99
{"type":"https://shoplite.example/problems/not-found","title":"Resource not found","status":404,"detail":"Product 99 was not found.","instance":"/api/v1/products/99"}
$ curl -si http://localhost:8080/api/v1/products/99 | head -1
HTTP/1.1 404 Not Found

What this code does

  • With -s alone you see only a JSON body. It is easy to miss that it describes an error.
  • The status line says 404 Not Found. Check the code first, then decide what the body means.

With curl -s, or in code that only reads the body, an error can look like data: {"title":"Resource not found","status":404,...} is valid JSON, and a careless client may try to show it as a product.

Decide from the status code first: 2xx means use the body, 4xx means fix the request, 5xx means retry later or report. When you test with curl, use -i so the status line is always in front of you.

Mistake 3: missing or malformed headers

A correct token sent without its scheme, then with Bearer.

Example 3 · Mistake: an Authorization header without its scheme

Terminal
curl -i https://api.fest.example/v1/admin/stats -H "Authorization: admin-token"curl -s https://api.fest.example/v1/admin/stats -H "Authorization: Bearer admin-token"
Sample output (the example fest API from the status codes lesson)
$ curl -i https://api.fest.example/v1/admin/stats -H "Authorization: admin-token"
HTTP/1.1 401 Unauthorized
Date: Fri, 16 Oct 2026 02:53:57 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 39
WWW-Authenticate: Bearer realm="fest", error="invalid_token"

{"error":"Token is invalid or expired"}
$ curl -s https://api.fest.example/v1/admin/stats -H "Authorization: Bearer admin-token"
{"products":8,"orders":0}

What this code does

  • The token is right, but the value lacks the Bearer scheme, so the server cannot read it: 401, and WWW-Authenticate adds error="invalid_token".
  • With Bearer, one space and the token, the same call returns the stats.

Three headers cause most "it works in Postman but not in my code" problems. A JSON body without Content-Type: application/json gets 415; an Accept the server cannot satisfy gets 406; an Authorization value without its scheme, or with a colon after it, gets 401 even with a valid token.

When a request fails for no clear reason, run it with curl -v and read the > lines: they show the headers that really went out, not the ones you meant to send.

Mistake 4: curl and shell slips

A search sent with -X GET and a body.

Example 4 · Mistake: -X GET with a body

Terminal
curl -s http://localhost:8080/echo -X GET -d '{"q":"redmi"}'
Sample output
GET /echo HTTP/1.1
Host: localhost:8080
User-Agent: curl/8.21.0
Accept: */*
Content-Length: 13
Content-Type: application/x-www-form-urlencoded

{"q":"redmi"}

What this code does

  • curl obeys -X GET and still sends the body, labelled as form data. Many servers ignore a GET body, so the search word never counts.
  • The fix is the query string: curl -s "http://localhost:8080/api/v1/products?q=redmi".

The shell reads your command before curl does, and curl options are case-sensitive. The usual slips:

  • Unquoted URL with & or ? → the shell cuts it; quote every URL with a query string.
  • -x instead of -X → "Could not resolve proxy"; curl in PowerShell → the Invoke-WebRequest alias.
  • Inline JSON in Windows PowerShell 5.1 → quotes stripped; send a file with --json "@body.json".
  • -X GET with -d → a GET with a body; move the input to the query string.

Finish up

Mark complete & continue

Next: Mini project: explore a public API with curl