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

500 Internal Server Error on attempting to comment on a PR via REST API

Alastair (they/them) September 11, 2024

When sending requests such as

"{\"content\":{\"raw\":\"wip\"},\"inline\":{\"from\":22,\"path\":\"ABD.DBS.Common/Decorators/BulkObservableCollection.cs\"}}"

to https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/pullrequests/{pull_request_id}/comments , I always get Internal Server Error 500 responses.

{\"type\": \"error\", \"error\": {\"message\": \"Something went wrong\", \"id\": \"8ecedff1d537444283badd7607d910f3\"}}

 Please could you advise as to what I'm doing incorrectly.

3 answers

1 accepted

0 votes
Answer accepted
Alastair (they/them) September 19, 2024

turned out to be an issue with the way I was using rust's serde_json and reqwest crates: supplying already serialized text from serde_json::to_string to reqwest's reqeustbuilder::json method was causing the correctly serialized text to be escaped and wrapped a 2nd time.

0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 16, 2024

Hi Alastair and welcome to the community!

You need to use the to parameter (instead of the from one).

I get a successful request with data like the following:

"{\"content\":{\"raw\":\"wip\"},\"inline\":{\"to\":16,\"path\":\"folder.ABC/fileA.txt\"}}"

Does this work for you or are you still experiencing issues?

Kind regards,
Theodora

Alastair (they/them) September 16, 2024

No luck using `to` instead of `from` -- it still gives the same 500 error

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 17, 2024

Hi Alastair,

I think your implementation does not send the data correctly then. If you have curl installed on your computer or another server with internet access, you can double-check by running a curl call from a terminal application like the following:


curl --request POST \
--url 'https://api.bitbucket.org/2.0/repositories/workspace-id/repo/pullrequests/pr-id/comments' \
--header 'Authorization: Bearer token' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data "{\"content\":{\"raw\":\"wip\"},\"inline\":{\"to\":22,\"path\":\"ABD.DBS.Common/Decorators/BulkObservableCollection.cs\"}}"

where workspace-id, repo, pr-id, and token, replace with your respective values.

This is the same data you posted in your question, except the "from" parameter that I have changed to "to". If this works, then your code is sending different data that makes the request fail.

Kind regards,
Theodora

Alastair (they/them) September 19, 2024

okay, I've managed to test the api, turns out to be an issue with my implementation; i was passing the message body through an additional level of json formatting that wasn't being shown in my logging. have now fixed the issue

0 votes
Jim Knepley - ReleaseTEAM
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 11, 2024

I assume you're filling in the path parameters appropriately, and your API token grants permission to comment on a pull request.

Your request body is missing the required "type" attribute. I think the value you want for that is "pullrequest" in this case.

Alastair (they/them) September 14, 2024

I have the following permissions, of which I think that "pullrequest:write" covers me for commenting.

"x-oauth-scopes": "pipeline, snippet, wiki, issue:write, pullrequest:write, project:write, team:write, account" 

 for headers, i'm providing the following

{
"authorization": "Bearer %ACCESSKEY%",
"accept": "application/json",
"content-type": "application/json"
}

which match the values provided in the documentation.

adding a type attribute by sending the following changes the response from a 500 to a 400 Bad Request error.

{\"type\":\"pullrequest\",\"content\":{\"raw\":\"wip\"},\"inline\":{\"from\":22,\"path\":\"ABD.DBS.Common/Decorators/BulkObservableCollection.cs\"}}

 

Jim Knepley - ReleaseTEAM
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 16, 2024

I agree that `pullrequest:write` will permit you to comment on a PR because it implicitly grants `pullrequest`. If all you're doing is commenting, you only need `pullrequest`.

https://developer.atlassian.com/cloud/bitbucket/rest/intro/#pullrequest-write

The backslashes in your request body are appropriate if you're using a language where you wrap the request in double quotes, but they wouldn't be valid for the raw content you're sending to the server. For example, in Python:

json_body = "{\"type\": \"pullrequest\"}"

...would be fine, because Python would need those backslashes to say "This is a literal double quote, not terminating the quote that started this string."

If that's what's being sent to the API, the backslashes will mean the same thing, and the JSON parser will fail with an error like "expected property name" or something similar, which would make sense for a 400 "Bad Request" error.

Alastair (they/them) September 16, 2024

I'm using Rust, the attached body is the output from logging the json serialized object made using serde. I wouldn't expect this to have any issues with incorrectly escaping the text unnecessarily, it's likely just due to the logging?

Jim Knepley - ReleaseTEAM
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 16, 2024

Excellent choice, and I agree that it could be a logging thing.

My next step would be to try to reproduce the request with something like Postman and see if that is enlightening at all.

Alastair (they/them) September 19, 2024

checking with postman was helpful -- turns out I was passing serde_json serialized text (that I was logging) into a method that would then serialize it again, which clearly broke the format.

Jim Knepley - ReleaseTEAM
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 19, 2024

Thanks for posting that discovery. It makes sense in hindsight, but it wasn't something I had considered.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events