500 Internal Server Error on attempting to comment on a PR via REST API
Asked by Alastair _they_them_:

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](https://api.bitbucket.org/2.0/repositories/%WORKSPACE%/%REPO%/pullrequests/%PR_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.


Answers:
author: Alastair _they_them_
body: 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.


author: Theodora Boudale
body: 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

comments: author: Alastair _they_them_
body: No luck using \`to\` instead of \`from\` -- it still gives the same 500 error


author: Theodora Boudale
body: 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


author: Alastair _they_them_
body: 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


author: Jim Knepley - ReleaseTEAM
body: 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.

comments: author: Alastair _they_them_
body: 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\"}}
```


author: Jim Knepley - ReleaseTEAM
body: 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.


author: Alastair _they_them_
body: 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?


author: Jim Knepley - ReleaseTEAM
body: 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.


author: Alastair _they_them_
body: 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.


author: Jim Knepley - ReleaseTEAM
body: Thanks for posting that discovery. It makes sense in hindsight, but it wasn't something I had considered.


Tags: bitbucket-cloud

cloud