首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Paste.ee api上传

Paste.ee api上传
EN

Stack Overflow用户
提问于 2017-09-30 16:26:14
回答 1查看 688关注 0票数 0

我试图通过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} --这是我正在使用的代码:

代码语言:javascript
运行
复制
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上输入了一个条目。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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字典。你也许该去做

代码语言:javascript
运行
复制
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

然后调试你得到的问题。

您链接到的文档中的示例是:

代码语言:javascript
运行
复制
curl "https://api.paste.ee/v1/pastes"
  -H "X-Auth-Token: meowmeowmeow"

因此,这就是paste.ee auth头看起来的样子。

代码语言:javascript
运行
复制
headers = {'X-Auth-Token': 'token'}

因此,您的整个程序应该看起来像

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46504731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档