How to add comments in jira issue using rest API?

Hai all, I have an issue when try to add comments in jira issue using rest API in forge custom UI app.
My code to add comment in jira issue is,

let bodyData = {
                    "visibility": {
                        "type": "role",
                        "value": "Administrators"
                    },
                    "body": {
                        "type": "doc",
                        "version": 1,
                        "content": [{
                            "type": "paragraph",
                            "content": [{
                                "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget venenatis elit. Duis eu justo eget augue iaculis fermentum. Sed semper quam laoreet nisi egestas at posuere augue semper.",
                                "type": "text"
                            }]
                        }]
                    }
                }
                const response1 = await api.asApp().requestJira('/rest/api/3/issue/{issue_id}/comment', {
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json'
                    },
                    body: bodyData
                });
                console.log(`Response: ${response1.status} ${response1.statusText}`);
                console.log(response1);
                console.log(await response1.json());

When i try it in postman with basic auth it works fine and create comment in jira issue. When i try the same in forge custom UI app, i got bad request(400 error). The error is,

{
errorMessages: [
‘Can not deserialize instance of com.atlassian.jira.issue.fields.rest.json.beans.CommentJsonBean out of START_ARRAY token\n’ +
’ at [Source: org.apache.catalina.connector.CoyoteInputStream@6f26ee48; line: 1, column: 1]’
]
}

Shall i need to change anything in my code? Why this problem occurs? If anyone know the solution to this issue, kindly share your answers.

Thanks in advance…

Hi @SuryaA ,

Your code has '/rest/api/3/issue/{issue_id}/comment' for the URL which doesn’t look like it’s set up for substituting in the correct issue ID or key. Were you POSTing to the correct URL?

Also, you may want to double check whether you should use api.asUser() instead of api.asApp(). If this is executed in the context of a UI, then api.asUser() is generally preferable.

Regards,
Dugald

Hai @dmorrow Really thanks for your reply. I referred the below doc to add comment in jira issue

https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-issue-issueidorkey-comment-post

Is anything wrong in this doc?
I got the current issue id using context and use it in the URL. Also i tried both api.asApp() and api.asUser(). Both of them are not worked.

@dmorrow The complete response to this api call is,

{
text: [Function: text],
json: [Function: json],
arrayBuffer: [Function: arrayBuffer],
headers: {
server: [ ‘envoy’ ],
‘cache-control’: [ ‘no-cache, no-store, no-transform’ ],
‘content-type’: [ ‘application/json;charset=UTF-8’ ],
‘content-encoding’: [ ‘gzip’ ],
‘strict-transport-security’: [ ‘max-age=315360000; includeSubDomains; preload’ ],
date: [ ‘Tue, 13 Jul 2021 05:48:25 GMT’ ],
‘x-request-id’: [ ‘0145bdd88137f3ad’ ],
‘x-arequestid’: [ ‘ed53b1ba-32fb-420e-a1d1-f1550960e30e’ ],
‘x-aaccountid’: [ ‘608a6064f55824007021c5f0’ ],
‘x-xss-protection’: [ ‘1; mode=block’ ],
‘transfer-encoding’: [ ‘chunked’ ],
‘x-envoy-upstream-service-time’: [ ‘49’ ],
‘timing-allow-origin’: [ ‘*’ ],
‘x-content-type-options’: [ ‘nosniff’ ],
connection: [ ‘close’ ],
‘x-trace-id’: [ ‘0145bdd88137f3ad’ ],
‘set-cookie’: [
‘atlassian.xsrf.token=dee2acff-2e27-42e8-a60f-d4dce842f605_0005666d37287c6dff397ac01fece6d769853b12_lin; Path=/; Secure’
],
‘x-frame-options’: [ ‘SameOrigin’ ],
‘expect-ct’: [
‘report-uri=“https://web-security-reports.services.atlassian.com/expect-ct-report/global-proxy”, enforce, max-age=86400’
]
},
ok: false,
status: 400,
statusText: ‘Bad Request’
}

Hai @dmorrow Sorry it’s totally my mistake. I need to put whole bodyData inside ( ). Now it’s working fine.

Hi @SuryaA ,

The body parameter needs to be a JSON string so change the following line:

body: bodyData

to:

body: JSON.stringify(bodyData)

Regards,
Dugald

Hai @dmorrow I used quotations in bodyData as mentioned in the doc that i send. That problem is fixed now.