我在使用外部API时遇到了这个问题。我把我的身体数据作为字典直接发送到请求中,得到了400个错误:
data = {
"someParamRange": {
"to": 1000,
"from": 100
},
"anotherParamRange": {
"to": True,
"from": False
}
}
当我添加了json.dumps wrap时,它可以工作:
data = json.dumps({
"someParamRange": {
"to": 1000,
"from": 100
},
"anotherParamRange": {
"to": True,
"from": False
}
})
号
我不完全理解为什么这是必要的,因为字典和JSON对象在语法上是相同的。有人能帮我了解这里的幕后情况吗?
为了完整起见,下面是我的标题:
headers = {'API-KEY': 'blerg', 'Accept-Encoding': 'UTF-8', 'Content-Type': 'application/json', 'Accept': '*/*', 'username': 'user', 'password': 'pwd'}
编辑:
我之前没有提到这个,但现在我觉得这可能是相关的。我使用的是python请求库,另一篇文章似乎建议您不必对请求对象的参数进行编码:https://stackoverflow.com/a/14804320/1012040
不管是get/post还是get/post,您都不必再次对参数进行编码,它只需要一个字典作为参数,这样做很好。
似乎不需要序列化?
我的请求对象:
response = requests.post(url, data=data, headers=headers)
。
您应该发布其余代码。我可以制作一个HTTP API,其中任何一个都可以等效地工作。
它不是公共API,O/W我会:(你是说这可能只是特定API的函数,而不是HTTP请求的一般函数吗?
我建议阅读对象序列化。json.dump序列化对象,以便实质上可以将其作为数据流发送。直接插入对象将通过服务器不知道如何解码的二进制数据流发送。
repr(dict)与json不同,因为前者使用单引号。
显然,您的API需要JSON编码,而不是表单编码数据。当您将一个dict作为data参数传入时,数据是形式编码的。当您传递一个字符串(如json.dumps的结果)时,数据不是表单编码的。
请考虑请求文档中的此报价:
Typically, you want to send some form-encoded data — much like an HTML form. To do this, simply pass a dictionary to the data argument. Your dictionary of data will automatically be form-encoded when the request is made.
There are many times that you want to send data that is not form-encoded. If you pass in a string instead of a dict, that data will be posted directly.
For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data:
号
>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}
>>> r = requests.post(url, data=json.dumps(payload))
参考文献:
http://www.w3.org/tr/html401/interact/forms.html h-17.13.3.4
http://docs.python requests.org/en/latest/user/quickstart/更复杂的post请求
这对我来说并不适用。我必须添加以下标题:headers=""content-type":"application/json""
请求库在版本2.4.2中添加了一个json参数。现在可以直接通过dictionary对象requests.post(url, json=data, headers=headers)。
虽然它们在语法上似乎是一个独立的,但有一点不同:json是序列化对象的字符串表示;在本例中,python dict.在本例中,您需要以字符串的形式发送序列化数据,因此json.dumps是执行序列化所必需的。
编辑
正如对问题的注释中所建议的那样,它是相对于已使用的API的,但是,在通过线路发送对象的过程中,必须在某个地方进行序列化。