Parsing error when trying to add JSON codeblock in comment via API

Hi there,

I am trying to add a comment via the Jira Cloud REST API that include a codeblock of JSON and keep getting a “400: Error parsing JSON”.

When I test the call JSON in Postman, it works as expected. But when I implement via Python, it throws the error and I cannot figure out why. I have even tested writing the Python JSON to a file and putting that into Postman, which works.

Here is the JSON payload:

{
  "body": {
    "type": "doc",
    "version": 1,
    "content": [
      {
        "type": "codeBlock",
        "attrs": {
          "language": "json"
        },
        "content": [
          {
            "type": "text",
            "text": "{\n  \"result_status_code\": \"204\"\n}"
          }
        ]
      }
    ]
  }
}

Here is the test code I am using (note: other calls use the same URL and headers and work):

url = config["jira"]["host"] + "/rest/api/3/issue/" + key + "/comment"

auth = HTTPBasicAuth(config["jira"]["email"], config["jira"]["api_token"])
headers = {
	"Accept": "application/json",
	"Content-Type": "application/json"
}
comment = {
		"body": {
			"type": "doc",
			"version": 1,
			"content": [
				{
					"type": "codeBlock",
					"attrs": {
						"language": "json"
					},
					"content": [
						{
							"type": "text",
							"text": "{\n  \"result_status_code\": \"204\"\n}"
						}
					]
				}
			]
		}
	}

print(comment)
print(url)

response = requests.request(
		"POST",
		url,
		data=comment,
		headers=headers,
		auth=auth
	)

Any ideas?

Thanks,
Cam

Welcome to the Atlassian developer community @CamLawler,

I think it’s a nuance of Python requests and you should swap data for json:

response = requests.request(
		"POST",
		url,
		json=comment,
		headers=headers,
		auth=auth
	)

See the last example from Requests’s quick start on more complicated post requests.

1 Like

Thanks for the note @ibuchanan

I tried and same error. I pulled the python code for testing directly from https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-issue-issueidorkey-comment-post

@ibuchanan I was wrong! It worked. Thank you.