我试图通过api上传到paste.ee,但是,我一直收到错误:{"errors":[{"field":"sections","message":"The sections must be an array.","code":-1},{"field":"sections","message":"The sections must be an array.","code":-1},{"field":"sections","message":"The sections must be an array.","code":-1}],"success":false}
--这是我正在使用的代码:
import requests
payload = {'key': 'apikey', 'sections':[{'name':'Section1', 'syntax':'autodetect','contents':'name'}]}
post_response = requests.post(url='https://api.paste.ee/v1/pastes', data=payload)
print post_response.text
我做错了什么?下面是在他们的wiki上输入了一个条目。
发布于 2017-09-30 16:49:22
似乎您没有正确地使用requests
:参见http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests
例如,GitHub API v3接受JSON编码的POST/修补程序数据:导入json >>> url = 'https://api.github.com/some/endpoint‘>>>有效载荷={’一些‘:'data'} >>> r= requests.post(url,data=json.dumps(有效载荷)) 您也可以使用json参数(添加在2.4.2版中)直接传递它,而不是自己编码,它将自动编码: url = 'https://api.github.com/some/endpoint‘>>>有效载荷={’s‘:'data'} >>> r= requests.post(url,json=payload)
然而,您正在尝试直接发送Python字典。你也许该去做
import requests
payload = {'key': 'apikey', 'sections':[{'name':'Section1', 'syntax':'autodetect','contents':'name'}]}
post_response = requests.post(url='https://api.paste.ee/v1/pastes', json=payload)
print post_response.text
然后调试你得到的问题。
您链接到的文档中的示例是:
curl "https://api.paste.ee/v1/pastes"
-H "X-Auth-Token: meowmeowmeow"
因此,这就是paste.ee auth头看起来的样子。
headers = {'X-Auth-Token': 'token'}
因此,您的整个程序应该看起来像
import requests
payload = {'sections':[{'name':'Section1', 'syntax':'autodetect','contents':'name'}]}
headers = {'X-Auth-Token': 'token'}
post_response = requests.post(url='https://api.paste.ee/v1/pastes', json=payload, headers=headers)
print post_response.text
https://stackoverflow.com/questions/46504731
复制相似问题