Understanding HTTP and Web communication

What is HTTP?

Last updated: 2/28/2025

1 hour
Medium

Understanding HTTP and Web Communication

1. What is HTTP?

HTTP (HyperText Transfer Protocol) is the foundation of communication on the web. It is a request-response protocol used by clients (like browsers, mobile apps, and APIs) to communicate with servers.

How HTTP Works:

  1. A client sends a request to a server.
  2. The server processes the request and sends a response back.
  3. The response includes a status code, headers, and optional data (like JSON).

2. HTTP Request Structure

An HTTP request consists of:

  • Method (GET, POST, etc.)
  • URL (The endpoint being requested)
  • Headers (Metadata about the request)
  • Body (Optional, used for sending data)

Example HTTP Request:

GET /books HTTP/1.1 Host: example.com Accept: application/json

Example HTTP Request with Data (POST):

POST /books HTTP/1.1 Host: example.com Content-Type: application/json { "title": "Clean Code", "author": "Robert C. Martin" }

3. HTTP Methods

HTTP defines different methods for interacting with resources:

MethodDescription
GETRetrieve data (Read)
POSTCreate a new resource
PUTUpdate/replace a resource
PATCHPartially update a resource
DELETERemove a resource

Example API Endpoints:

ActionHTTP MethodEndpoint
Get all booksGET/books
Get a specific bookGET/books/:id
Create a new bookPOST/books
Update a bookPUT/books/:id
Delete a bookDELETE/books/:id

4. HTTP Response Structure

A server's response includes:

  • Status Code (Indicates success or error)
  • Headers (Metadata)
  • Body (Contains requested data)

Example Response:

HTTP/1.1 200 OK Content-Type: application/json [ { "id": 1, "title": "Clean Code", "author": "Robert C. Martin" } ]

5. HTTP Status Codes

Status codes indicate the result of a request.

Common Status Codes:

CodeMeaning
200 OKSuccess
201 CreatedResource created
400 Bad RequestInvalid request from client
401 UnauthorizedAuthentication required
403 ForbiddenAccess denied
404 Not FoundResource does not exist
500 Internal Server ErrorServer failure

6. Headers in HTTP Requests and Responses

Headers provide metadata about a request or response.

Common Headers:

HeaderDescription
Content-TypeSpecifies the format of the body (e.g., application/json)
AuthorizationUsed for authentication (e.g., API keys, JWT)
AcceptSpecifies the response format the client expects
Cache-ControlControls caching behavior

Example of Headers in an API Request:

GET /books HTTP/1.1 Host: example.com Accept: application/json Authorization: Bearer <token>

7. Testing HTTP Requests

You can test HTTP requests using:

  • Postman (GUI tool)
  • cURL (Command-line tool)
  • Your browser (For simple GET requests)

Example cURL Request:

curl -X GET "https://example.com/books" -H "Accept: application/json"

8. Summary

  • HTTP is the foundation of web communication.
  • Clients make requests and servers send responses.
  • Different HTTP methods (GET, POST, PUT, DELETE) define how we interact with APIs.
  • Status codes indicate the result of the request.
  • Headers help manage data format, authentication, and caching.

Next Lesson: Writing Your First RESTful API - "Hello World"