-
-
Notifications
You must be signed in to change notification settings - Fork 0
Parts of the HTTP header
Every HTTP message starts with a line that tells us the broad meaning of the message, followed by zero or more header fields.
At a high level, it looks like this:
A B C
X: Y
The first line means different things depending on whether the message is a request or a response.
For an HTTP request:
-
Ais the request method, such asGETorPOST -
Bis the request target, such as/index.htmlor/api/user?id=5 -
Cis the protocol version, such asHTTP/1.1
Example:
GET /articles?page=2 HTTP/1.1
Host: example.com
Accept: text/html
For an HTTP response:
-
Ais the protocol version, such asHTTP/1.1 -
Bis the numeric status code, such as200or404 -
Cis the reason phrase, such asOKorNot Found
Example:
HTTP/1.1 404 Not Found
Content-Type: text/plain
Content-Length: 9
After the start line come the header fields.
-
Xis the header name, such asContent-Type -
Yis the header value, such asapplication/json
Each header field is one name/value pair:
Content-Type: application/json
Cache-Control: no-store
Some header values themselves contain multiple parts. For example:
Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8
That is still one header field. The commas are part of the value syntax.
In phpgt/http:
- the request start line becomes the request method, target URI, and protocol version on
Request - the response start line becomes the status code and protocol version on
Response - the header fields are stored in
Headers,RequestHeaders, orResponseHeaders
The GT\Http\Header\Parser class can parse a raw response-style header block into:
- protocol version
- status code
- header key/value pairs
Most repeated headers can be merged safely, but Set-Cookie should stay as separate lines.
This library preserves that distinction, which is why cookie headers are treated slightly differently from normal comma-combinable headers.
Understanding the shape of an HTTP message makes the rest of the library much easier to follow. Once you can recognise the start line, the header fields, and the body as separate concerns, the purpose of Request, Response, Headers, and Stream becomes much clearer.
Learn about FormData and URLSearchParams to work with body data more comfortably.
PHP.GT/Http is a separately maintained component of PHP.GT/WebEngine.
- Creating requests from global state
- Request and ServerRequest objects
- Response objects
- URI objects
- Working with headers
- Parts of the HTTP header