我可以用 curl 运行这个 curl 状态并且工作得很好。我读过很多帖子,但没有任何效果。
curl -X POST "http://some.website.com" -H "accept: application/json" -H "authorization: Basic authcode" -H "Content-Type: application/json" -d "{ "Fields": [ "string" ], "Filters": [ { "Field": "Item", "Operator": "=", "Value": "119001" } ], "PageSize": 0, "PageNumber": 0}"
到目前为止尝试的代码
import requests
session = requests.Session()
url = 'http://some.website.com'
headers = {'accept': 'application/json', 'authorization': 'Basic authcode', 'Content-Type': 'application/json'}
data = {'Fields': 'string', 'Filters': { 'Field': 'Item', 'Operator': '=', 'Value': '119001' }, 'PageSize': 0, 'PageNumber': 0}
response = session.post(url, headers=headers, data=data)
print(response.status_code)
print(response.json())
错误 = 无效的 JSON 值
我也试过
import simplejson as json
# ...
# ...
response = session.post(url, headers=headers, data=json.dumps(data))
# ...
# ...
失败 = 检测 JSON 字段时出错
我认为这与嵌套的字典语句有关
使用 https://httpbin.org/post 我可以看到服务器上接收了哪些数据(标头和正文(,并且我看到相同的结果curl
curl -X POST "http://httpbin.org/post" -H "accept: application/json" -H "authorization: Basic authcode" -H "Content-Type: application/json" -d "{"Fields": ["string"], "Filters": [{"Field": "Item", "Operator": "=", "Value": "119001"}], "PageSize": 0, "PageNumber": 0}"
# result
{
"args": {},
"data": "{"Fields": ["string"], "Filters": [{"Field": "Item", "Operator": "=", "Value": "119001"}], "PageSize": 0, "PageNumber": 0}",
"files": {},
"form": {},
"headers": {
"Accept": "application/json",
"Authorization": "Basic authcode",
"Content-Length": "122",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0"
},
"json": {
"Fields": [
"string"
],
"Filters": [
{
"Field": "Item",
"Operator": "=",
"Value": "119001"
}
],
"PageNumber": 0,
"PageSize": 0
},
"origin": "83.23.32.69, 83.23.32.69",
"url": "https://httpbin.org/post"
}
和Python
(使用json=data
或data=json.dumps(data)
代替data=data
(
import requests
headers = {
'Accept': 'application/json',
'Authorization': 'Basic authcode',
# 'Content-Type': 'application/json',
# 'User-Agent': 'Mozilla/5.0',
}
data = {
"Fields": [ "string" ],
"Filters": [ { "Field": "Item", "Operator": "=", "Value": "119001" } ],
"PageSize": 0,
"PageNumber": 0
}
response = requests.post('https://httpbin.org/post', headers=headers, json=data)
print(response.text)
# result
{
"args": {},
"data": "{"Fields": ["string"], "Filters": [{"Field": "Item", "Operator": "=", "Value": "119001"}], "PageSize": 0, "PageNumber": 0}",
"files": {},
"form": {},
"headers": {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate",
"Authorization": "Basic authcode",
"Content-Length": "122",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0"
},
"json": {
"Fields": [
"string"
],
"Filters": [
{
"Field": "Item",
"Operator": "=",
"Value": "119001"
}
],
"PageNumber": 0,
"PageSize": 0
},
"origin": "83.23.32.69, 83.23.32.69",
"url": "https://httpbin.org/post"
}
只有标题的区别:"User-Agent": "curl/7.58.0"
和"User-Agent": "python-requests/2.22.0"
。而Python使用"Accept-Encoding": "gzip, deflate"
。
BTW:您可以使用门户 curl.trillworks.com 将curl
转换为 Python 代码