Creating a PR comment via REST API
Asked by anton.merzliakov:

Greetings.

I am trying to add a comment to my PR. I tried **both** the method from the documntation and from Google. **Both** return **required key not provided** and do not create a comment. As you can see, the error message doesn't tell me which required key is not provided.

The json from documentation is a long one

```
{
  "type": "pullrequest_comment",
  "id": 2154,
  "created_on": "2023-06-28T12:44:36.726033+00:00",
  "updated_on": "2023-06-28T12:44:36.726033+00:00",
  "content": {
    "raw": "test text",
    "markup": "plaintext",
    "html": ""
  },
  "user": {
    "type": "user"
  },
  "deleted": False,
  "inline": {
    "from": 57,
    "to": 122,
    "path": ""
  },
  "pullrequest": {
   "type": "pullrequest"
  },
  "resolution": {
    "type": "<string>",
    "user": {
    "type": "user"
  },
  "created_on": "2023-06-28T12:44:36.726033+00:00"
}
```

<br />

As I can see from the documentation, this json has a required parameter **resolution-\>type** , but the documentation doesn't mention what are allowed values for this r**esolution-\>type** . There's also **inline-\>path** that is also required and I also have no idea what to write as a path. The most stupid part is that the key **content** is not marked as required -- why would I create a comment with no content? But a random key **resolution** is required?  

The simpler json I tried is just  

```
{
  "content": {
    "raw": "first!"
  }
}
```

I found it both on Stackoverflow and Atlassian Community. I have no idea why it worked for those people because this json clearly doesn't have a bunch of required parameters.


Answers:
author: Patrik S
body: Hello [@anton.merzliakov](/t5/user/viewprofilepage/user-id/5258949) and welcome to the Community!

You should be able to create a new comment in a Pull Request just providing the raw content, as in your example. However, it's important to include the header **Content-Type: application/json**in the request, otherwise it might fail with a bad request error as you received.

You can use the following example as a base to make your request :

```
curl -X POST -u USERNAME:APP_PASSWORD 'https://api.bitbucket.org/2.0/repositories/<workspace>/<Repository>/pullrequests/<Pull Request ID>/comments' --header 'Content-Type: application/json' --data '{"content":{"raw":"testing one comment"}}'
```

With this command, you should be able to successfully create a comment in the Pull Request ID provided in the URL.

Hope that helps! Let us know in case you run into any issues.

Thank you, [@anton.merzliakov](/t5/user/viewprofilepage/user-id/5258949) !

Patrik S

comments: author: anton.merzliakov
body: It actually worker! Really thank you a lot, Patrik!

(It would also be nice if the documentation had this example.)

Maybe you have any idea what is happening with Atlassian's Python library too? <https://community.atlassian.com/t5/Bitbucket-questions/Create-a-PR-comment-using-Python/qaq-p/2407830#M95382>


author: David Bakkers
body: This is the risk you take when using third party libraries to 'short cut' your development work. It may or may not be maintained it to the exact extent needed.

If in doubt, always use the native functionality in your development language to interact with the REST APIs, and always use a test tool like Postman to validate your query before assuming your code is faulty.

PS. Atlassian, the company, doesn't own or maintain that python library. It's a public library that is maintained by volunteers. It's just called the 'Atlassian' library because it's used with Atlassian's products. If you have found a real bug with it, log it on [its GitHub page](https://github.com/atlassian-api/atlassian-python-api).


author: anton.merzliakov
body: Well, I did try the official way first. It was nice that Patrik understood what was wrong with my request. The documentation can definitely be improved.


Tags: bitbucket-cloud

cloud

rest-api