Multiline comment on pull request via API 2.0

Chris Smith April 11, 2019

I am trying to post a comment that includes newlines to a pull request using https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/pullrequests/%7Bpull_request_id%7D/comments 

 

My request body looks like this: 

{"content":{"raw":"Report\nTotal Files Examined: 20\nLooks Good!"}}

I would expect the UI comment to look like this:

Report
Total Files Examined: 20
Looks Good!

The request is accepted, but the output in the pull request looks like this:

Report Total Files Examined: 20 Looks Good!

If I open the HTML of the PR page, I see the following:

<p>Report
Total Files Examined: 20
Looks Good!</p>

I believe what is happening is that the raw content is always being pasted into the <p> tags, but what *should* happen is every newline is converted into a new <p> tag. 

 

I have also tried using \\n and <br>, \\n turns into this in the UI

Report\nTotal Files Examined: 20\nLooks Good!

and <br> turns into this:

Report<br>Total Files Examined: 20<br>Looks Good!

 

Is there any way around this? I need to UI to respect the line breaks somehow.

2 answers

1 accepted

1 vote
Answer accepted
Chris Smith April 12, 2019

I discovered I can send using markdown and am using " \n" (space and newline) to make line breaks, this creates the multilines as I want.

###Report \nTotal Files Examined: 20 \nLooks Good!

 becomes

REPORT

Total Files Examined: 20
Looks Good!

Deleted user July 3, 2019

How did you get this to work? I'm trying to send markdown and it's not working. I tried specifying that it is markup and this results in an error:

 

test.sh "This is a test\nthis is only a test\n\`here is some code\`"

{"content":{"raw":"This is a test\nthis is only a test\n`here is some code`","markup":"markdown"}}

{"type": "error", "error": {"fields": {"content.markup": "extra keys not allowed"}, "message": "Bad request"}}

Chris Smith July 3, 2019

@[deleted] The API docs seem to be incorrect (though they're correct for the UI API).

 

I sent as raw without a markup parameter and as I said above with a space character before the newline.

 

So in your case: 

{"content":{"raw":"This is a test\nthis is only a test\n`here is some code`","markup":"markdown"}}

becomes

{"content":{"raw":"This is a test \nthis is only a test \n`here is some code`"}}

 

That should work

Deleted user July 8, 2019

I tried that (space before newline) and it didn't make a difference.

Chris Smith July 8, 2019

the "extra keys" is due to the markup parameter. In my example I am also removing it. Did you do that too?

Deleted user July 9, 2019

Yep...the comment posted but there were no line breaks...just run on text.

Jose Elizondo September 6, 2019

@[deleted] any luck with that multiline text? I've tried without success, I do send text as markup and it's rendered as markup but no line breaks (API 2.0).

-d '{"content":{"raw":"**Result** \nDeployed."}}'

Mihnea GALCA January 16, 2020

-d '{"content":{"raw":"**Result** \n  \nDeployed."}}'

New-line is created by \n##\n where # is space

Like Anton Plebanovich likes this
Anton Plebanovich August 12, 2021

Two spaces worked for me, thanks!

Eduard Mutin May 13, 2023

Unfortunately, two spaces didn't work for me but \n\r works:

curl --request POST --header 'Content-Type:application/json' -d '{"content":{"raw": "line 1\n\rline 2"}}' https://api.bitbucket.org/2.0/repositories/$BITBUCKET_WORKSPACE/$BITBUCKET_REPO_SLUG/pullrequests/$BITBUCKET_PR_ID/comments?access_token=$ACCESS_TOKEN
0 votes
deepak.soni
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!
March 7, 2024
It worked for me, you can also try this 
var bodyData = JSON.stringify({
      "body": {
        "content": [
          {
            "content": [
              {
                "text": "Please join my remote session at:\n",
                "type": "text"
              },
              {
                "type": "text",
                "text": "https://join.gotoresolve.com/" + pinCode + "\n",
                "marks": [
                  {
                    "type": "link",
                    "attrs": {
                    "href": "https://join.gotoresolve.com/" + pinCode
                    }
                  }
                ]
              },
              {
                "text": "Support Key: " + getSplitText(pinCode),
                "type": "text"
              }
            ],
            "type": "paragraph"
          }
        ],
        "type": "doc",
        "version": 1
      }
    });

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events