Fundamentals 11 min read

Mastering HTTP: Complete Guide to Protocol Structure, Methods, and Status Codes

This article provides a thorough overview of the HTTP protocol, covering its basic format, workflow, request and response message structures, common methods, status codes, and URL composition, complete with examples and code snippets to help developers understand and use HTTP effectively.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Mastering HTTP: Complete Guide to Protocol Structure, Methods, and Status Codes

HTTP Protocol Overview

HyperText Transfer Protocol (HTTP) is an application‑layer protocol that underpins data communication on the World Wide Web. It defines how clients and servers exchange requests and responses over a reliable transport layer, typically TCP.

Basic Format and Versions

The generic URL format is protocol://serverIP[:port]/path[?query]. HTTP/1.1 was standardized in RFC 2616 (1999), and HTTP/2 replaced it with RFC 7540 (2015).

Key Characteristics

Stateless: each request is independent, though cookies and sessions can add state.

Multiple requests per connection: browsers first fetch HTML, then additional resources; HTTP/2 supports multiplexing.

Connection handling: HTTP/1.0 closes after each request, while HTTP/1.1 keeps the connection alive for a timeout period.

Built on TCP: the protocol specifies message format, not transport details.

HTTP Workflow

A client opens a TCP connection to the server (default port 80), sends an HTTP request, receives a response, and then closes the connection (or reuses it). If a domain name is used, DNS resolution occurs first.

Request Message Structure

An HTTP request consists of four parts: request line, headers, a blank line, and an optional body.

# Request line
POST /index.html HTTP/1.1
# Headers
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:67.0)
Accept: text/html,application/xhtml+xml,application/xml;
Accept-Language: zh-CN,zh;
Accept-Encoding: gzip, deflate
Referer: http://127.0.0.1/index.html
Content-Type: application/x-www-form-urlencoded
Content-Length: 29
Connection: close
Cookie: security=impossible; PHPSESSID=8vv0n11btuol45hqcm5recmfp7
Upgrade-Insecure-Requests: 1

# Body
username=admin&password=admin

Response Message Structure

An HTTP response also has four parts: status line, headers, a blank line, and the response body.

# Status line
HTTP/1.1 200 OK
# Headers
Date: Tue, 10 Aug 2021 09:09:09 GMT
Content-Length: 5185
Connection: close
Content-Type: text/html;charset=gb2312

# Body
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">...

HTTP Methods

HTTP/1.1 defines eight standard methods, all uppercase:

GET : Retrieve a resource without side effects.

HEAD : Same as GET but returns only headers.

POST : Submit an entity body to the server.

PUT : Upload a complete replacement for a resource.

DELETE : Remove the specified resource.

TRACE : Echo the received request for diagnostics.

OPTIONS : Query the communication options supported by the target.

CONNECT : Establish a tunnel, typically for SSL through a proxy.

If a method is unsupported, the server returns 405; if unimplemented, 501. At minimum, a server should implement GET and HEAD.

HTTP Status Codes

Status codes indicate the result of a request. They are grouped as follows:

1xx – Informational

2xx – Success (e.g., 200 OK)

3xx – Redirection (e.g., 302 Found)

4xx – Client errors (e.g., 400 Bad Request, 404 Not Found, 401 Unauthorized, 403 Forbidden)

5xx – Server errors (e.g., 500 Internal Server Error, 503 Service Unavailable)

URL Composition

A typical URL consists of:

Protocol (e.g., http)

Host name or IP address

Optional port (default 80 for HTTP)

Path (e.g., /blog/index.html)

Query string starting with ? (e.g., ?id=10&page=1)

Fragment identifier starting with # (optional)

Example: http://www.example.com:80/blog/index.html?id=10&page=1.

Conclusion

The article consolidates essential knowledge about HTTP, emphasizing its usage scenarios, request/response formats, workflow, methods, status codes, and URL structure, providing developers with a solid foundation for working with web communications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

HTTPStatus CodesNetworkingURLweb protocolmethodsRequest/Response
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.