Sobes.tech
Junior

What components does an HTTP request consist of?

sobes.tech AI

Answer from AI

The HTTP request consists of the following components:

  1. Request Line:

    • HTTP Method (GET, POST, PUT, DELETE, etc.).
    • Resource path (URL).
    • HTTP protocol version (HTTP/1.1, HTTP/2.0, etc.).
    // Example of request line:
    GET /index.html HTTP/1.1
    
  2. Headers:

    • Send additional information about the request, client, or resource.
    • Each header is a key-value pair.
    // Examples of headers:
    Host: www.example.com
    User-Agent: Mozilla/5.0 (...)
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    
  3. Empty line:

    • Separates headers from the request body.
    // Empty line between headers and request body
    
  4. Request Body:

    • Contains data sent to the server, if applicable (e.g., in POST or PUT methods).
    • The format of the body is determined by the Content-Type header.
    // Example of request body in JSON format:
    {
      "name": "John Doe",
      "occupation": "Software Engineer"
    }