Menu

Request headers

“The extra information a client sends along with a request.”

View .md

After the first line come headers: Name: value pairs, one per line, ending with an empty line.

http
1GET /api/profile HTTP/1.1
2Host: api.example.com
3Accept: application/json
4Accept-Language: nl, en;q=0.8
5Authorization: Bearer eyJhbGciOi...

Header names are case-insensitive: Accept and accept are the same.

Headers you’ll see all the time#

HeaderExampleTells the server…
Hostapi.example.comwhich website you want (one server can host many). Required in HTTP/1.1
User-Agentcurl/8.7.1what software is asking
Acceptapplication/jsonwhat formats you can handle
Accept-Languagenl, en;q=0.8preferred languages (q = preference from 0 to 1)
Content-Typeapplication/jsonthe format of the body you’re sending
AuthorizationBearer <token>who you are
Cookiesession=abc123cookies the server set earlier

Cookies: how “stateless” HTTP remembers you#

Remember that HTTP is stateless. Cookies fix that:

  1. You log in. The server answers with a Set-Cookie header:
    http
    HTTP/1.1 200 OK
    Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Lax
  2. From now on, the browser adds that cookie to every request to the same site:
    http
    1GET /account HTTP/1.1
    2Host: shop.example.com
    3Cookie: session=abc123
  3. The server looks up abc123 and knows it’s you.

Caution

Anyone who gets that cookie value is you as far as the server knows. HttpOnly hides it from JavaScript and Secure sends it only over HTTPS. Use both for session cookies.

Sending headers yourself#

bash
curl -H "Accept: application/json" -H "Accept-Language: nl" https://httpbin.org/headers

httpbin.org/headers echoes back the headers it received, so you can see exactly what was sent:

json
1{
2  "headers": {
3    "Accept": "application/json",
4    "Accept-Language": "nl",
5    "Host": "httpbin.org",
6    "User-Agent": "curl/8.7.1"
7  }
8}

Byte check

0/3

Answer these 3 questions to commit this byte to your learning path.

  1. Which header tells the server the format of the body you’re sending?
  2. After you log in, how does the server recognize you on the next request?
  3. Which cookie flag hides a session cookie from JavaScript?
Search posts and bytes

Optional account

Keep your progress everywhere

Everything on AminBytes is free to read, with or without an account. Sign in only if you want your commits, streak and merged topics saved, on every device.

or

We keep only your name, avatar and which bytes you finished, and you can delete it all anytime. Privacy