我想请求Bitbucket API创建仓库。下面的curl命令起作用:
curl -v -X POST -d '{"scm": "git", "is_private": "true", "fork_policy": "no_forks", "project": {"key": "MARS"}}' -H "Content-Type: application/json" https://api.bitbucket.org/2.0/repositories/myteam/test -u <user-name>
因此,我在python中使用请求进行了同样的尝试:
data = {'scm': 'git', 'is_private': 'true', 'fork_policy': 'no_forks', 'project': {'key': 'MARS'}}
auth=(user, password)
headers = {"Content-Type": "application/json"}
url = "https://api.bitbucket.org/2.0/repositories/myteam/test"
res = requests.post(url, data=data, headers=headers, auth=auth)
但是res
返回'Bad request‘(错误请求)。为什么?
发布于 2017-06-29 18:29:06
从您的curl
请求可以明显看出,Bitbucket正在接受JSON编码的POST数据。使用requests
作为表单编码数据发送数据会导致HTTP Error 400 Bad request。
为了作为JSON编码的POST数据发送,请使用:
requests.post(url, json=data, headers=headers, auth=auth)
参考资料:
http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests
https://stackoverflow.com/questions/44832338
复制相似问题