GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python GET/POST request - send GET and POST requests with urllib, urllib3, requests, socket; handle them in Flask Python GET/POST requestlast modified October 30, 2026 Python 3.15
In this article we show how to send GET and POST requests in Python. In the
examples, we use the standard HTTPThe Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web.
In the examples, we use HTTP GETThe HTTP GET method requests a representation of the specified resource. GET requests:
HTTP POSTThe HTTP POST method sends data to the server in the body of the request. It is often used when uploading a file or when submitting a completed web form. POST requests:
Installing the libraries
The $ pip install urllib3 requests Flask GET request with socketHTTP is a text protocol on top of TCP. To understand what the libraries do for us, we first send a GET request through a plain socket. socket_get.py
import socket
host = 'example.com'
request = (
'GET / HTTP/1.0\r\n'
f'Host: {host}\r\n'
'Accept: text/html\r\n'
'\r\n'
)
with socket.create_connection((host, 80), timeout=10) as s:
s.sendall(request.encode('ascii'))
chunks = []
while data := s.recv(4096):
chunks.append(data)
response = b''.join(chunks).decode()
head, _, body = response.partition('\r\n\r\n')
print(head)
print()
print(body[:60])
We connect to port 80 of the server and send the request line, the
$ python socket_get.py HTTP/1.1 200 OK Date: Fri, 30 Oct 2026 20:24:01 GMT Content-Type: text/html; charset=utf-8 Connection: close Server: cloudflare Last-Modified: Thu, 29 Oct 2026 13:15:03 GMT Allow: GET, HEAD Accept-Ranges: bytes Age: 2230 cf-cache-status: HIT CF-RAY: a52d24227804b379-PRG alt-svc: h3=":443"; ma=86400 <!doctype html><html lang=en><head><meta charset=utf-8><link GET request with urllib.request
The urllib_get.py
from urllib.request import urlopen
with urlopen('https://example.com', timeout=10) as resp:
print(resp.status, resp.reason)
print(resp.headers['Content-Type'])
html = resp.read().decode('utf-8')
start = html.index('<title>') + len('<title>')
end = html.index('</title>')
print(html[start:end])
The returned response object is a context manager. We print the status
code, the reason phrase, and the content type, and then read the body. The
$ python urllib_get.py 200 OK text/html; charset=utf-8 Example Domain Query parameters with urllib.request
The parameters of a GET request are sent in the query string of the URL.
The urllib_get_params.py
import json
from urllib.parse import urlencode
from urllib.request import urlopen
params = {'name': 'John Doe', 'occupation': 'gardener'}
url = 'https://httpbin.org/get?' + urlencode(params)
print(url)
with urlopen(url, timeout=10) as resp:
data = json.load(resp)
print(data['args'])
print(data['url'])
The space in $ python urllib_get_params.py
https://httpbin.org/get?name=John+Doe&occupation=gardener
{'name': 'John Doe', 'occupation': 'gardener'}
https://httpbin.org/get?name=John+Doe&occupation=gardener
POST request with urllib.request
To send a POST request with urllib_post.py
import json
from urllib.parse import urlencode
from urllib.request import Request, urlopen
form = {'name': 'John Doe', 'occupation': 'gardener'}
body = urlencode(form).encode('ascii')
req = Request('https://httpbin.org/post', data=body, method='POST')
with urlopen(req, timeout=10) as resp:
data = json.load(resp)
print(data['form'])
print(data['headers']['Content-Type'])
The form data is encoded with $ python urllib_post.py
{'name': 'John Doe', 'occupation': 'gardener'}
application/x-www-form-urlencoded
GET request with urllib3
The urllib3_get.py
import urllib3
resp = urllib3.request('GET', 'https://httpbin.org/get',
fields={'name': 'John Doe', 'occupation': 'gardener'})
print(resp.status)
print(resp.headers['Content-Type'])
data = resp.json()
print(data['args'])
print(data['headers']['User-Agent'])
The response object has a $ python urllib3_get.py
200
application/json
{'name': 'John Doe', 'occupation': 'gardener'}
python-urllib3/2.8.0
POST request with urllib3
A urllib3_post.py
import urllib3
http = urllib3.PoolManager(timeout=10.0, retries=2)
resp = http.request('POST', 'https://httpbin.org/post',
fields={'name': 'John Doe', 'occupation': 'gardener'},
encode_multipart=False)
print(resp.json()['form'])
resp = http.request('POST', 'https://httpbin.org/post',
json={'name': 'John Doe', 'tags': ['garden', 'flowers']})
data = resp.json()
print(data['json'])
print(data['headers']['Content-Type'])
For POST requests, $ python urllib3_post.py
{'name': 'John Doe', 'occupation': 'gardener'}
{'name': 'John Doe', 'tags': ['garden', 'flowers']}
application/json
GET request with requests
A GET request is generated with the requests_get.py
import requests
params = {'name': 'John Doe', 'occupation': 'gardener'}
resp = requests.get('https://httpbin.org/get', params=params, timeout=10)
print(resp.status_code)
print(resp.url)
print(resp.json()['args'])
The $ python requests_get.py
200
https://httpbin.org/get?name=John+Doe&occupation=gardener
{'name': 'John Doe', 'occupation': 'gardener'}
POST request with requests
A POST request is generated with the requests_post.py
import requests
url = 'https://httpbin.org/post'
resp = requests.post(url, data={'name': 'Peter'}, timeout=10)
data = resp.json()
print(data['form'])
print(data['headers']['Content-Type'])
resp = requests.post(url, json={'name': 'Peter', 'age': 34}, timeout=10)
data = resp.json()
print(data['json'])
print(data['headers']['Content-Type'])
The first request sends form data, the second one JSON data. The library sets the content type of the request accordingly. httpbin.org returns the JSON keys in alphabetical order. $ python requests_post.py
{'name': 'Peter'}
application/x-www-form-urlencoded
{'age': 34, 'name': 'Peter'}
application/json
Handling HTTP errors with requests
The requests library does not raise an exception for error status codes.
The requests_errors.py
import requests
for status in (200, 404, 503):
resp = requests.get(f'https://httpbin.org/status/{status}', timeout=10)
try:
resp.raise_for_status()
print(status, 'ok')
except requests.HTTPError as e:
print(e)
The $ python requests_errors.py 200 ok 404 Client Error: Not Found for url: https://httpbin.org/status/404 503 Server Error: Service Unavailable for url: https://httpbin.org/status/503 Process GET request in FlaskThe following example shows how to process a GET request in a Flask application. flask_get/app.py
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def index():
return 'Home page'
@app.get('/greet')
def greet():
name = request.args.get('name', 'Guest')
msg = f'Hello {name}'
return msg, 200, {'Content-Type': 'text/plain; charset=utf-8'}
The application creates and sends a message to the client. It uses the
value from the
We start the development server in the background and send GET requests
with the $ flask --app flask_get/app run & * Serving Flask app 'flask_get/app' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5000 Press CTRL+C to quit $ curl -i 'localhost:5000/greet?name=Lucia' HTTP/1.1 200 OK Server: Werkzeug/3.1.9 Python/3.15.2 Date: Fri, 30 Oct 2026 20:24:09 GMT Content-Type: text/plain; charset=utf-8 Content-Length: 11 Connection: close Hello Lucia 127.0.0.1 - - [30/Oct/2026 21:24:09] "GET /greet?name=Lucia HTTP/1.1" 200 - $ curl localhost:5000/greet Hello Guest 127.0.0.1 - - [30/Oct/2026 21:24:09] "GET /greet HTTP/1.1" 200 - Process POST request in FlaskThe following example shows how to process a POST request in Flask. The application accepts both form and JSON data. flask_post/app.py
from flask import Flask, request
app = Flask(__name__)
users = []
@app.post('/users')
def create_user():
if request.is_json:
user = request.get_json()
else:
user = request.form.to_dict()
if 'name' not in user:
return {'error': 'name is required'}, 400
users.append(user)
return user, 201
@app.get('/users')
def list_users():
return users
The
We send form data with the $ flask --app flask_post/app run &
* Serving Flask app 'flask_post/app'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
$ curl -i -d name=Peter -d occupation=driver localhost:5000/users
HTTP/1.1 201 CREATED
Server: Werkzeug/3.1.9 Python/3.15.2
Date: Fri, 30 Oct 2026 20:24:10 GMT
Content-Type: application/json
Content-Length: 39
Connection: close
{"name":"Peter","occupation":"driver"}
127.0.0.1 - - [30/Oct/2026 21:24:10] "POST /users HTTP/1.1" 201 -
$ curl -H 'Content-Type: application/json' -d '{"name": "Lucia", "occupation": "teacher"}' localhost:5000/users
{"name":"Lucia","occupation":"teacher"}
127.0.0.1 - - [30/Oct/2026 21:24:10] "POST /users HTTP/1.1" 201 -
$ curl -d occupation=pilot localhost:5000/users
{"error":"name is required"}
127.0.0.1 - - [30/Oct/2026 21:24:11] "POST /users HTTP/1.1" 400 -
$ curl localhost:5000/users
[{"name":"Peter","occupation":"driver"},{"name":"Lucia","occupation":"teacher"}]
127.0.0.1 - - [30/Oct/2026 21:24:11] "GET /users HTTP/1.1" 200 -
Sourceurllib.request - Python documentation In this article we have generated GET and POST requests in Python and processed them in Flask. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |