Junior — Middle
How can header data be separated from the main part of the request?
sobes.tech AI
Answer from AI
In HTTP requests, header data (headers) are separated from the main part (request body) by an empty line (double newline). This is the standard format of the HTTP protocol: headers come first, each on its own line, then an empty line, followed by the request body.
In PHP, when working with HTTP requests, for example, when using streams or parsing raw requests, you can split headers and body by this empty line.
Example of splitting a raw HTTP request:
$rawRequest = "POST /path HTTP/1.1\r\nHost: example.com\r\nContent-Type: application/json\r\n\r\n{"key":"value"}";
list($headerPart, $body) = explode("\r\n\r\n", $rawRequest, 2);
$headers = explode("\r\n", $headerPart);
// $headers now contains the headers, $body is the request body
Thus, the empty line (double newline) serves as a separator between headers and the request body.