GET & POST Methods
GET and POST Methods in Python
In RESTful architecture and web communications, HTTP verbs inform the destination web server of the intended action. The two most fundamental verbs are GET (retrieving data) and POST (submitting or creating data). The requests library simplifies configuring parameters, headers, and payloads for both.
1. GET Requests and Query Parameters
A GET request is idempotent and safe: it retrieves data from the server without modifying server-side state.
Passing Query Parameters with params
Instead of manually concatenating query strings (e.g., https://api.example.com/search?query=python&page=2), pass a Python dictionary to the params argument. The requests library automatically escapes special characters and builds the query string:
Notice how spaces became + and the ampersand & became %26 through automatic URL encoding!
2. POST Requests: Sending Data to the Server
A POST request transmits a payload in the HTTP request body to create or mutate server resources (e.g., submitting a registration form, charging an account, or creating a new blog post).
Option A: Sending JSON Payloads (json=...)
Most modern REST APIs expect payloads formatted as JSON. Passing a dictionary to the json parameter automatically:
- 1Serializes the dictionary into a JSON string via
json.dumps(). - 2Sets the
Content-Type: application/jsonHTTP header.
Option B: Sending Form Data (data=...)
When submitting HTML forms or traditional URL-encoded payloads, pass the dictionary to the data parameter:
3. Custom Request Headers
Custom headers allow you to define authentication tokens, API keys, custom user agents, or content negotiation preferences:
4. GET vs. POST: Architectural Comparison
| Dimension | GET Method | POST Method |
|---|---|---|
| Purpose | Retrieve existing resources | Create or process resources |
| Payload Location | URL query parameters (?key=val) | HTTP Request Body |
| Idempotent? | Yes (repeating produces same state) | No (repeating may create duplicate records) |
| Data Length | Limited by browser/server URL limits (~2048 chars) | Virtually unlimited |
| Caching & History | Cached by browsers, logged in server access logs | Never cached by default |
| Security | Sensitive data visible in URLs (browser history, logs) | Encrypted within TLS/HTTPS body payload |
Multiple Choice Questions
1. Which keyword argument should you pass to requests.get() to include URL query parameters cleanly?
A. query B. params C. data D. args Answer: B Explanation: The params parameter takes a dictionary and encodes it into URL query parameters (e.g. ?search=python).
2. What does requests.post(url, json=payload) do automatically?
A. Compresses the payload into a zip archive B. Serializes the dictionary to a JSON string and sets Content-Type: application/json C. Encrypts the payload with RSA keys D. Sends the request via FTP Answer: B Explanation: Using json=... serializes the dictionary to a JSON formatted string and sets the appropriate application/json header automatically.
3. Why should sensitive information like user passwords never be transmitted using a GET request?
A. GET requests are disabled over HTTPS B. Query parameters appear in plain text in browser histories, server access logs, and referral headers C. GET requests cannot send characters other than numbers D. The GET method requires root administrative privileges Answer: B Explanation: GET query parameters are part of the URL, making them visible in browser histories, proxy logs, and web server logs.
4. What is the difference between passing data to data= vs json= in requests.post()?
A. data= sends form-encoded (application/x-www-form-urlencoded) data, while json= sends JSON-encoded (application/json) data B. data= is used only for text files C. json= is deprecated in modern versions of requests D. There is no difference Answer: A Explanation: data= submits form-encoded or raw byte strings, whereas json= sends JSON-encoded data with matching headers.
5. How do you supply an API authentication token using an HTTP header in requests?
A. requests.get(url, auth_token="my_token") B. requests.get(url, headers={"Authorization": "Bearer my_token"}) C. requests.get(url, params={"auth": "my_token"}) D. requests.get(url, token="my_token") Answer: B Explanation: Standard HTTP authentication tokens (like Bearer tokens) are passed as key-value pairs inside the headers dictionary.
Parsing JSON Data
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| HTTP Requests using requests | Parsing JSON Data |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.