I want to make a request to Bitbucket API to create repository. The following curl works:
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>
So I tried the same in python using requests:
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)
but the res
returns 'Bad request' (400). why?
It is evident from your curl
request that Bitbucket is accepting JSON-Encoded POST data.
Sending data using requests
as form-encoded data results in a HTTP Error 400 Bad request.
In order to send as JSON-Encoded POST data, use:
requests.post(url, json=data, headers=headers, auth=auth)
http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests