0

So I have this repo: https://bitbucket.com/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/ this URL outputs alot of information about the repository

repo

I then tried to create a Python script which featch this URL information:

import requests

headers = {
    'Content-Type': 'application/json',
}

response = requests.get('https://bitbucket.com/rest/api/1.0/projects/GGT/repos/GRP1/', headers=headers, auth=('USERNAME', 'TOKEN'))
print(response.text)

This prints out the same JSON stuff, as shown in the picture.

Now I know I have a connection to the repository. I then want to add a Webhook to this repo, but I cant seem to find out how to do this.

I tried to do something like this, but that doesn't work.

json_data = {
    'title': 'BitBucket_Webhook',
    'url': 'http://uua231z:5000/bitbucket',
    'enabled': 'true',
}

response = requests.put('https://bitbucket.com/rest/webhook/1.0/projects/GGT/repos/GRP1/configurations', headers=headers, json=json_data, auth=('USERNAME', 'TOKEN'))
print(response)

How can I add a webhook to a repository, can't seem to find that much on the internet on how this could be possible?

I tried this from the comments

headers = {
    # Already added when you pass json= but not when you pass data=
    # 'Content-Type': 'application/json',
}

json_data = {
    'title': 'BitBucket_Webhook2',
    'url': 'http://test123:5000/bitbucket',
    'enabled': 'true',
}

# Found here: https://docs.atlassian.com/bitbucket-server/rest/4.1.0/bitbucket-rest.html
response = requests.put('https://bitbucket.com/rest/api/1.0/projects/GGT/repos/grp1/webhooks', headers=headers, json=json_data, auth=('USERNAME', 'TOKEN'))
print(response)

But I get <Response [405]>

1 Answer 1

0

You may want to check the official Atlassian documentation for the API. There is this HTTP (POST) /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/webhooks that might solve your needs.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried but I get the respone <Response [405]> which is Method not allowed. My account is admin on the website, and manually I can create these Webhooks. I posted the code for you to look at, if you want.
Hi, in your edited question i see you are using requests.put. In the docs, the method to invoke is a POST. Try `requests.post´ and see if it works now
If I just write requests.post I get response <Response [400]> which is Bad Request, but you are right, I should probably use the post and not put

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.