Understanding GET vs POST: HTTP Request Methods Explained with Real Examples
This article explains the fundamentals of HTTP GET and POST request methods, detailing their request line, headers, and body structures, illustrating each with real‑world examples and code snippets, and clarifying their safety, idempotence, caching, and practical differences.
Overview
The article summarizes the eight HTTP request methods, focusing on GET and POST, and outlines the structure of an HTTP request message.
HTTP Request Message Structure
An HTTP request consists of three parts:
Request line : method, URL, and HTTP version (e.g., GET /path?query HTTP/1.1).
Headers : zero or more header fields, each as Header-Name: value, terminated by CRLF.
Body (request data) : present for methods like POST; absent for GET.
Example of a GET request to GitHub’s API:
GET /search/users?q=JakeWharton HTTP/1.1
Host: api.github.com
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: _octo=...; _ga=...; logged_in=yes; dotcom_user=GoMarck; _gid=...; _gat=1The request line shows the GET method, the URL /search/users?q=JakeWharton, and HTTP/1.1. All subsequent lines are headers; there is no body.
Example of a POST request:
POST / HTTP/1.1
Host: www.wrox.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6) Gecko/20050225 Firefox/1.0.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 40
Connection: Keep-Alive
name=Professional%20Ajax&publisher=WileyHere the method is POST, the request body contains form data ( name=Professional%20Ajax&publisher=Wiley), separated from the headers by a blank line.
Characteristics of GET
Parameters are appended to the URL after a ? and separated by &.
Designed for safe, idempotent data retrieval; it should not modify server state.
Browsers may cache GET responses, reducing network traffic.
URL length is limited by browsers and servers, not by the HTTP spec.
Typically sends a single TCP packet containing both headers and (empty) body.
Characteristics of POST
Used to submit data that may change server resources (e.g., form submissions, likes).
Not considered safe or idempotent because it can modify state.
Request data resides in the body, allowing virtually unlimited size.
Usually involves two TCP packets: one for headers, another for the body after a 100 Continue response.
GET vs POST Differences
Although both are HTTP methods built on TCP, the protocol distinguishes them to simplify management: GET for retrieving resources, POST for creating or updating resources. Their practical differences arise from where parameters are placed (URL vs body), caching behavior, and compliance with safety and idempotence rules. While technically you could swap parameter locations, servers may or may not honor such non‑standard usage.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
