Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Creating a PR comment via REST API

Matthias Redies
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 19, 2022

I am trying to follow this example:

https://developer.atlassian.com/server/bitbucket/rest/v806/api-group-pull-requests/#api-api-latest-projects-projectkey-repos-repositoryslug-pull-requests-pullrequestid-comments-post

and I have modified my python code to this:

def create_comment(pr_id, comment):
    url = f"http://{baseurl}/rest/api/latest/projects/{projectKey}/repos/{repositorySlug}/pull-requests/{pr_id}/comments"
    headers = {"Accept": "application/json", "Content-Type": "application/json"}
    payload = json.dumps({"text": comment})

    response = requests.request(
        "POST",
        url,
        data=payload,
        headers=headers,
        verify=False,
        auth=HTTPKerberosAuth(mutual_authentication=OPTIONAL),
    )

    print(response.status_code)

    print(
        json.dumps(
            json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")
        )
    )

But as a response I always get:

400
{
    "errors": [
        {
            "context": null,
            "exceptionName": null,
            "message": "The path query parameter is required when retrieving comments."
        }
    ]
}

What am I doing wrong here? I am not trying to retrieve comments. I am trying to add a comment. I can add a ?path=/ to my url, but then I only get an empty list of comments.

1 answer

0 votes
Erez Maadani
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 19, 2022

Hey @Matthias Redies

I think the problem is with the python requests, which on redirect, transform the POST into a GET request.

My guess is that because you are using "http", you get a redirect (301 or 302) to "https", and then the method changes.

If that doesn't help, try adding allow_redirects=False to disable redirections from the server.

Hope that helps   

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events