我正在寻找使用Python请求库在GitHub存储库中创建新文件的方法。在命令行中输入以下内容对我有用(替换LOGIN
并TOKEN
相应地设置):
curl -X PUT -d '{"path": "testfile.txt", "message": "test", "content": "aGVsbG8y"}' https://api.github.com/repos/LOGIN/testrepo/contents/testfile.txt\?access_token\=TOKEN
但是在尝试与请求进行相同操作时,我一直遇到“问题解析JSON”错误(状态代码400):
data = {
"message": "test",
"content": "aGVsbG8y",
"path": "testfile.txt"
}
url = "https://api.github.com/repos/LOGIN/testrepo/contents/testfile.txt?access_token={}".format(TOKEN)
response = requests.put(url, data=data)
关于我正在做什么的任何暗示吗?我已经检查了类似的问题,但没有找到正确的调整方法。谢谢!
因为仅传递data
参数会自动将字典作为表单编码的参数发送。而是将其作为JSON传递
import json
data = {
"message": "test",
"content": "aGVsbG8y",
"path": "testfile.txt"
}
url = "https://api.github.com/repos/LOGIN/testrepo/contents/testfile.txt?access_token={}".format(TOKEN)
response = requests.put(url, data=json.dumps(data))
或者,如果您至少使用版本2.4.2,则可以这样做:
response = requests.put(url, json=data)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句