Important headers: Content-Type, Accept, Authorization, Location
35 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A request can have a perfect body and still be refused because of its labels. A few headers decide whether the server can read it, whether you can read the answer, and who is asking.
Content-Type and charset: what the body is
Content-Type is the header that names the format of a message body as a media type, written type/subtype, such as application/json, text/html or image/png. Requests and responses both use it.
Two requests with the same JSON body and no Content-Type: the API refuses the first, and /echo shows why.
Example 1 · Why curl -d gets 415: the default Content-Type
curl -i http://localhost:8080/api/v1/products -d '{"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":4,"price":449.00,"stock":15}'curl -s http://localhost:8080/echo -d '{"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":4,"price":449.00,"stock":15}'$ curl -i http://localhost:8080/api/v1/products -d '{"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":4,"price":449.00,"stock":15}'
HTTP/1.1 415 Unsupported Media Type
Date: Fri, 16 Oct 2026 02:43:30 GMT
Content-Type: application/problem+json
Content-Length: 193
Accept-Post: application/json
{"type":"https://shoplite.example/problems/unsupported-media-type","title":"Unsupported media type","status":415,"detail":"Content-Type must be application/json.","instance":"/api/v1/products"}
$ curl -s http://localhost:8080/echo -d '{"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":4,"price":449.00,"stock":15}'
POST /echo HTTP/1.1
Host: localhost:8080
User-Agent: curl/8.21.0
Accept: */*
Content-Length: 81
Content-Type: application/x-www-form-urlencoded
{"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":4,"price":449.00,"stock":15}What this code does
- The first request sends perfectly good JSON, but the API answers
415: the body type is wrong, not the content.Accept-Posttells you which type this URL does take. - The practice API's
/echoURL sends back the request exactly as it arrived. It shows the cause: without-H, curl labelled the bodyapplication/x-www-form-urlencoded. Adding-H "Content-Type: application/json"fixes it (Example 5).
Without Content-Type the receiver has to guess, and a strict API refuses to guess. A charset parameter says how the text becomes bytes, as in text/html; charset=utf-8; JSON is always UTF-8, so application/json alone is correct and ; charset=utf-8 is optional.
The classic trap: curl -d sends Content-Type: application/x-www-form-urlencoded (HTML form fields) unless you say otherwise, and a JSON API then answers 415 Unsupported Media Type.
Fix the error
Fix the header
Read the answer, then pick the line that fixes the request.
A JSON body is refused. Replace the wrong header line.
Error
HTTP/1.1 415 Unsupported Media Type Accept-Post: application/json
Accept and content negotiation: 406 and 415
Accept is the request header that lists the formats the client can read, such as Accept: application/json; */* means anything. Choosing a format from this list is called content negotiation.
One URL, asked for XML and then for JSON. This API can only produce JSON.
Example 2 · Accept: 406 for XML, 200 for JSON
curl -i http://localhost:8080/api/v1/products/6 -H "Accept: application/xml"curl -i http://localhost:8080/api/v1/products/6 -H "Accept: application/json"$ curl -i http://localhost:8080/api/v1/products/6 -H "Accept: application/xml"
HTTP/1.1 406 Not Acceptable
Date: Fri, 16 Oct 2026 02:43:31 GMT
Content-Length: 0
$ curl -i http://localhost:8080/api/v1/products/6 -H "Accept: application/json"
HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 02:43:31 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 141
ETag: "p6-v1"
Cache-Control: no-cache
{"id":6,"sku":"ACC-AD141","name":"boAt Airdopes 141","categoryId":4,"categoryName":"Accessories","price":1299.00,"stock":120,"isActive":true}What this code does
- This API can only produce JSON. A client that accepts only XML gets
406 Not Acceptable, with an empty body: the server cannot even write an error in a format the client accepts. - With
Accept: application/jsonthe same URL works. Most clients sendAccept: */*orapplication/json, so 406 is rare in practice.
A client can list several types with a preference weight from 0 to 1, called a q-value: Accept: text/html, application/json;q=0.9 means HTML first, JSON second. The server picks the best match it can produce.
Two codes come from a mismatch. 406 Not Acceptable: the server cannot send any format in your Accept list. 415 Unsupported Media Type: the server cannot read the format in your Content-Type. Tip: 406 is about the response, 415 about the request body.
Authorization: Basic and Bearer, only over HTTPS
Authorization is the request header that carries the client's credentials as <scheme> <credentials>. Basic sends a Base64-encoded username and password; Bearer sends a token the server gave you after login.
Basic auth sent with curl -u and decoded back with one command, then a Bearer token on an admin URL.
Example 3 · Basic auth is only Base64
curl -s http://localhost:8080/echo -u asha:fest2026echo YXNoYTpmZXN0MjAyNg== | base64 -d$ curl -s http://localhost:8080/echo -u asha:fest2026 GET /echo HTTP/1.1 Host: localhost:8080 Authorization: Basic YXNoYTpmZXN0MjAyNg== User-Agent: curl/8.21.0 Accept: */* $ echo YXNoYTpmZXN0MjAyNg== | base64 -d asha:fest2026
What this code does
-u user:passwordmakes curl build the Basic header itself. The echo shows what really travelled:Authorization: Basic YXNoYTpmZXN0MjAyNg==.- One
base64 -dturns it back intoasha:fest2026. Anyone who can read the request can read the password, which is why Basic auth is safe only inside HTTPS.
Example 4 · A Bearer token opens the admin stats
curl -i https://api.fest.example/v1/admin/stats -H "Authorization: Bearer admin-token"HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 02:43:33 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 25
{"products":8,"orders":0}What this code does
- The scheme
Bearer, one space, then the token. With an admin token the answer is200; in the status codes lesson the same URL gave401with no token and403with a learner token. - Real tokens are long random strings or signed JWTs, not words like
admin-token. This example uses short ones so the commands stay readable.
Base64 turns any bytes into plain letters and digits. It is an encoding, not encryption: anyone who sees a Basic header can decode the password in one command. A Bearer token is a long random string; whoever holds ("bears") it is let in until it expires.
Both schemes travel in readable form, so send them only over HTTPS, which encrypts the whole message. Keep tokens out of URLs, logs and Git. Getting and refreshing tokens is covered in the State, Cookies & Sessions module.
Predict the output
Read the Authorization header
Choose the true statement.
Round 1 of 2
What does this header carry?
Location: where the new resource lives
Location is a response header that holds a URL: after 201 Created it points to the resource just made, and on a 3xx redirect it is the address to go to next.
Create the tripod, then fetch it from the address the server gave.
Example 5 · Follow Location after 201 Created
curl -i http://localhost:8080/api/v1/products -H "Content-Type: application/json" -d '{"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":4,"price":449.00,"stock":15}'curl -s http://localhost:8080/api/v1/products/16$ curl -i http://localhost:8080/api/v1/products -H "Content-Type: application/json" -d '{"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":4,"price":449.00,"stock":15}'
HTTP/1.1 201 Created
Date: Fri, 16 Oct 2026 02:43:33 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 134
Location: http://localhost:8080/api/v1/products/16
{"id":16,"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":4,"categoryName":"Accessories","price":449.00,"stock":15,"isActive":true}
$ curl -s http://localhost:8080/api/v1/products/16
{"id":16,"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":4,"categoryName":"Accessories","price":449.00,"stock":15,"isActive":true}What this code does
- With the right
Content-Type, the same body from Example 1 is accepted:201 CreatedwithLocation: http://localhost:8080/api/v1/products/16. - The client GETs that URL as it is. The id came from the server; the client never guessed it.
The value may be a full URL, such as ShopLite's http://localhost:8080/api/v1/products/16, or just a path such as /get, which means on the same server. A client that receives a 201 should read Location instead of guessing the id.
Other headers you will meet
User-Agent is the request header that names the client program and its version, such as curl/8.21.0 or a browser string. Servers log it, but anyone can fake it, so never trust it for security.
One request with its own User-Agent, a custom header and a Cache-Control header.
Example 6 · User-Agent, a custom header and Cache-Control
curl -s http://localhost:8080/echo -A "ShopLite-Android/2.4" -H "X-Request-Id: 3f9a1c" -H "Cache-Control: no-cache"GET /echo HTTP/1.1 Host: localhost:8080 User-Agent: ShopLite-Android/2.4 Accept: */* X-Request-Id: 3f9a1c Cache-Control: no-cache
What this code does
-Areplaces curl's ownUser-Agentwith the name an Android app might send. The server sees only what the client claims, so it is useful for logs and statistics, never for security.X-Request-Idis a custom header the server can copy into its logs to trace this one request.Cache-Control: no-cachein a request asks caches on the way to check with the server instead of answering from a saved copy.
A few more headers appear in almost every exchange:
Content-Length: body size in bytes (tools set it for you).Cache-Control: caching rules, such asno-store(never keep a copy) ormax-age=3600(fresh for an hour);no-cachemeans "check with the server before reusing".- Custom headers: an API may define its own, like
X-Request-Idto trace one request through the logs. TheX-prefix is an old habit; newer headers drop it (Idempotency-Key). Date(when the response was made) andAllow(methods a URL supports) you have already met.
Next step
Check your understanding
12 quick questions