When I do a curl request for GET endpoint of a REST API, I would go it like this
curl -X 'GET' \
'http://localhost:8000/user/?limit=10' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '[
{
"item_type": "user",
"skip": 0
}
]'
Directly translated into requests
, I would get
import requests
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
}
params = {
'limit': '10'
}
json_data = [
{
'item_type': 'user',
'skip': 0,
},
]
response = requests.get('http://localhost:8000/user/', params=params, headers=headers, json=json_data)
# Note: json_data will not be serialized by requests
# exactly as it was in the original request.
#data = '[\n {\n "item_type": "document",\n "skip": 0\n }\n]'
#response = requests.get('http://localhost:8000/user/', params=params, headers=headers, data=data)
Now for my test environment I am going to use httpx
's AsyncClient
, but I cannot find a way to use the data part, i.e., [{"item_type": "user", "skip": 0}]
, in a get request.
It would be terrific, if you know how to address this.
According to the httpx docs,
.get
doesn't support request bodies. It suggests you use the more generic.request
function instead.Important note: the HTTP spec states clients should not use body content with
GET
requests and if they do, there's no guarantee that it will show up at the server (as intermediaries are allowed to strip it off):