Network Responses

Understanding how servers respond to your network requests is crucial for building robust and efficient applications. This section details the various components of an HTTP response.

HTTP Response Structure

An HTTP response is the message sent back from the server to the client (e.g., your browser) after processing a request. It typically consists of three parts:

  1. Status Line: The first line, indicating the HTTP version, status code, and a text description.
  2. Headers: Key-value pairs providing metadata about the response (e.g., Content-Type, Content-Length).
  3. Body: The actual data being sent back, such as HTML, JSON, images, etc.

Here's a visual representation:

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Length: 1234

<!DOCTYPE html>
<html>
<head>
    <title>Example Page</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

HTTP Status Codes

Status codes are three-digit codes that indicate the result of a request. They are grouped into five classes:

1xx Informational

Requests received, continuing process.

2xx Success

The action was successfully received, understood, and accepted.

200 OK
201 Created
204 No Content
3xx Redirection

Further action needs to be taken by the user agent in order to complete the request.

301 Moved Permanently
302 Found
304 Not Modified
4xx Client Error

The request contains bad syntax or cannot be fulfilled.

400 Bad Request
401 Unauthorized
403 Forbidden
404 Not Found
5xx Server Error

The server failed to fulfill an apparently valid request.

500 Internal Server Error
503 Service Unavailable

Common Response Headers

Response headers provide essential context about the request and the server's capabilities:

Content-Type

Indicates the media type of the resource. Examples:

  • text/html for HTML documents
  • application/json for JSON data
  • image/png for PNG images
  • text/css for CSS stylesheets
  • application/javascript for JavaScript files

Often includes a character set, like text/html; charset=UTF-8.

Content-Length

The size of the response body in bytes. This helps the client determine how much data to expect.

Date

The date and time at which the message was originated.

Server

Contains information about the software used by the origin server to fulfill the request.

Set-Cookie

Used to send cookies from the server to the client. This is essential for session management.

Cache-Control

Directives for caching mechanisms, such as how long a resource can be cached or if it should be revalidated.

Location

Used in redirection responses (3xx) to indicate the URL of the resource to redirect to.

Response Body

The response body is the actual content returned by the server. Its format is determined by the Content-Type header and the nature of the request.

HTML Responses

When requesting a web page, the server typically returns HTML content, which the browser then renders.

<!DOCTYPE html>
<html>
<head>
    <title>Document Title</title>
</head>
<body>
    <p>This is the content of the page.</p>
</body>
</html>

JSON Responses

Common for APIs, JSON (JavaScript Object Notation) is a lightweight data-interchange format.

{
    "message": "Data retrieved successfully",
    "data": {
        "id": 123,
        "name": "Example Item"
    }
}

Other Formats

Servers can return various other types of data, including:

  • XML (application/xml)
  • Images (e.g., image/jpeg, image/png)
  • Videos (e.g., video/mp4)
  • Plain text (text/plain)
  • CSS (text/css)
  • JavaScript (application/javascript)