Skip to lesson

Request line, headers and body

A request that fails with 400 is hard to fix when you cannot see what was really sent. Every request and response has the same plain-text shape, and reading it is the first debugging skill.

The request line: method, target and version

Request line is the first line of every HTTP request. It has three parts separated by single spaces: the method, the target and the HTTP version, as in GET /api/v1/products/6 HTTP/1.1.

A first request written out by hand: a request line and one header. Below it is what the server sends back.

A request and a response have the same four parts, in the same order. A request has a request line such as GET /api/v1/products/6 HTTP/1.1, then header lines such as Host and Accept, then a blank line, then an optional body, which a GET leaves empty.

Example 1 · A request line and the Host header

Text
GET /api/v1/products/6 HTTP/1.1Host: localhost:8080
Sample response
HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 02:30:40 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

  • GET is the method, /api/v1/products/6 the target and HTTP/1.1 the version, with exactly one space between them.
  • The request ends after Host with a blank line, because a GET has no body. The response starts with its own first line, HTTP/1.1 200 OK, then headers, a blank line and the product as JSON.

The method says what to do (GET), the target says which thing (/api/v1/products/6) and the version is HTTP/1.1. The target is the path and query of the URL without scheme and host, so http://localhost:8080/api/v1/products?q=redmi becomes /api/v1/products?q=redmi; the host travels in a header.

Each line of the head ends with two invisible characters, carriage return and line feed, written \r\n and called CRLF. Tools add them for you; you only notice them when you build a message by hand.

The messages on this page are real runs against a small ShopLite practice API on localhost:8080. You build one like it in the Tiny HTTP Server module; the curl lesson shows public test services you can practise on until then.

Headers: name: value lines, and the required Host

Header is one line of extra information in a request or response, written Name: value, such as Accept: application/json. Header names are case-insensitive; header values usually are not.

Two requests: one with unusual capitals in the header names, and one with no Host header at all.

Example 2 · Header names ignore case

Text
GET /api/v1/products/6 HTTP/1.1host: localhost:8080ACCEPT: application/json
Sample response
HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 02:30:41 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

  • host and ACCEPT are written in other cases than usual, and the server still reads them: header names are case-insensitive.
  • The answer is the same 200 OK as in Example 1. Write names in the usual form (Host, Accept) anyway; it is easier for people to read.

Example 3 · Forget Host and the server refuses

Text
GET /api/v1/products/6 HTTP/1.1accept: application/json
Sample response
HTTP/1.1 400 Bad Request
Date: Fri, 16 Oct 2026 02:30:42 GMT
Content-Type: application/problem+json
Content-Length: 178
Connection: close

{"type":"https://shoplite.example/problems/malformed-request","title":"Malformed request","status":400,"detail":"The request has no Host header.","instance":"/api/v1/products/6"}

What this code does

  • The request line is fine, but the Host header is missing, so this is not a valid HTTP/1.1 request. The server answers 400 Bad Request before it even looks at the path.
  • Connection: close tells the client that the server is closing the connection after this answer, because it cannot trust the rest of what it received. The body is ShopLite's standard error format, which the status codes lesson explains.

Headers come right after the first line, one per line, in any order. Copy values exactly, because only the names ignore case.

HTTP/1.1 requires one header in every request: Host, the server name and port from the URL. One computer often runs many websites on the same IP address, and Host tells it which one you mean; without it the server answers 400 Bad Request.

HTTP/2, which most HTTPS sites use, sends the same headers in a compact binary form with lowercase names. The meaning does not change, so everything on this page still applies.

The blank line, the body and Content-Length

Content-Length is the header that gives the size of the body in bytes, so the receiver knows where the body ends. The sender sets it; the receiver cannot guess it.

A POST sends data after the headers. The blank line ends the headers, and Content-Length says how many bytes of body follow.

Example 4 · A POST with a JSON body and Content-Length

Text
POST /api/v1/products HTTP/1.1Host: localhost:8080Content-Type: application/jsonContent-Length: 97{"sku":"ACC-KB100","name":"Zebronics Wireless Keyboard","categoryId":4,"price":999.00,"stock":30}
Sample response
HTTP/1.1 201 Created
Date: Fri, 16 Oct 2026 02:30:43 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 150
Location: http://localhost:8080/api/v1/products/16

{"id":16,"sku":"ACC-KB100","name":"Zebronics Wireless Keyboard","categoryId":4,"categoryName":"Accessories","price":999.00,"stock":30,"isActive":true}

What this code does

  • The body {"sku":"ACC-KB100",...} is 97 bytes long, so Content-Length: 97. Content-Type: application/json tells the server to read it as JSON. The blank line separates the headers from the body.
  • When the same body was sent with Content-Length: 40, the server read only the first 40 bytes, found broken JSON and answered 400 Bad Request with a malformed-request error.
  • The answer is 201 Created: the product was stored with id 16, and Location: http://localhost:8080/api/v1/products/16 says where to find it. The server filled in categoryName and isActive itself.

Whatever follows the blank line is the body, such as a new product in JSON. A GET usually has no body, so it ends at the blank line. Content-Type says what kind of data the body holds, such as application/json.

Count bytes, not letters. English letters take one byte each in UTF-8, but ₹ takes three. A Content-Length that is too small cuts the body short; one that is too large leaves the server waiting for bytes that never come. curl, browsers and HTTP libraries work it out for you; you write it yourself only when you type a raw message.

Order of execution

Build the request, then read the answer

Tap the parts in the order they travel.

The response: status line, headers and body

Status line is the first line of an HTTP response: the version, a three-digit status code and a short reason phrase, as in HTTP/1.1 201 Created. Programs act on the number; the phrase is only for people.

Fetch product 16 with curl -i, which prints the whole response, not only the body.

Example 5 · Read a response with curl -i

Terminal
curl -i http://localhost:8080/api/v1/products/16
Sample output
HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 02:30:46 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 150
ETag: "p16-v1"
Cache-Control: no-cache

{"id":16,"sku":"ACC-KB100","name":"Zebronics Wireless Keyboard","categoryId":4,"categoryName":"Accessories","price":999.00,"stock":30,"isActive":true}

What this code does

  • Line 1 is the status line: version HTTP/1.1, status code 200, reason phrase OK.
  • Next come the response headers: Content-Type and Content-Length: 150 describe the body, Date is when the server answered. ETag and Cache-Control help with caching, which the status codes lesson uses for 304 Not Modified.
  • After the blank line comes the body: exactly 150 bytes of JSON, the product created in Example 4.

A response has the same shape as a request. After the status line come the headers: Content-Type and Content-Length describe the body, Date says when the server sent it, and after a create, Location gives the address of the new item. A blank line and the body follow.

The curl lesson at the end of this module covers curl -i and the other options in detail.

Reading a full request and response pair

curl -v is curl in verbose mode: it prints both messages of one exchange. Lines that start with > were sent by curl, lines that start with < came back, and lines that start with * are curl's own notes about the connection.

One POST sent with curl -v, so the request and the response appear together.

Example 6 · A whole exchange with curl -v

Terminal
curl -v http://localhost:8080/api/v1/products -H "Content-Type: application/json" -d '{"sku":"ACC-KB200","name":"Zebronics Wired Keyboard","categoryId":4,"price":499.00,"stock":50}'
Sample output
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:8080...
* Established connection to localhost (::1 port 8080) from ::1 port 53734 
* using HTTP/1.x
> POST /api/v1/products HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.21.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 94
> 
* upload completely sent off: 94 bytes
< HTTP/1.1 201 Created
< Date: Fri, 16 Oct 2026 02:30:46 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 147
< Location: http://localhost:8080/api/v1/products/17
< 
* Connection #0 to host localhost:8080 left intact
{"id":17,"sku":"ACC-KB200","name":"Zebronics Wired Keyboard","categoryId":4,"categoryName":"Accessories","price":499.00,"stock":50,"isActive":true}

What this code does

  • The > lines are the request curl built: POST (chosen because of -d), the target, Host, User-Agent, Accept, your Content-Type and a Content-Length: 94 that curl counted for you. The > line with nothing after it is the blank line.
  • The < lines are the response head: 201 Created, then the headers, including Location: http://localhost:8080/api/v1/products/17. The body is printed last.
  • The * lines are notes, not HTTP: name lookup, the connection to ::1 (the IPv6 form of localhost) port 8080, and the connection kept open for reuse. Your local port number will differ.

Read the output in a fixed order: the request line (what was asked), the request headers and body, the status line (did it work), then the response headers and body. Where to look:

  • > request line and headers, then the body curl uploaded
  • < status line and response headers
  • The response body is printed last, after curl's closing * note

Next step

Check your understanding

12 quick questions