Network Basics
Network Basics
HyperText Transfer Protocol (HTTP)
HTTP Request Message
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import requests
api_url = "https://example.com/api"
params = {'key_1': 'value_1', 'key_2': 'value_2'}
header = {'User-Agent': 'MyApp/1.0', 'Authorization': 'Bearer SomeToken'}
data = {'usr_name': 'JohnDoe', 'usr_pwd': 'SomePassword'}
response = requests.post(
url,
params=params,
headers=headers,
data=data, # or json=data
)
This will generate the following HTTP request message
1
2
3
4
5
6
7
8
9
POST /api?key_1=value_1&key_2=value_2 HTTP/1.1
Host: example.com
User-Agent: MyApp/1.0
Authorization: Bearer token123
Content-Type: application/x-www-form-urlencoded
Content-Length: 30
username=john&password=secret
# if using json: {'usr_name': 'JohnDoe', 'usr_pwd': 'SomePassword'}
This post is licensed under CC BY 4.0 by the author.