Middle
Tell me about the structure of an HTTP request and response.
sobes.tech AI
Answer from AI
An HTTP request consists of three main parts:
-
Request Line — contains the method (GET, POST, etc.), the resource URI, and the protocol version.
-
Headers — key-value pairs that convey meta-information, such as content type, cookies, authorization.
-
Body — optional part that contains data, for example, in POST requests.
Example of a request:
POST /api/users HTTP/1.1
Host: example.com
Content-Type: application/json
Content-Length: 48
{"name":"Ivan","email":"ivan@example.com"}
An HTTP response also consists of three parts:
-
Status Line — protocol version, status code (e.g., 200 OK).
-
Headers — meta-information about the response, such as content type, length.
-
Body — data returned by the server, such as HTML, JSON.
Example of a response:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 27
{"id":123,"status":"ok"}