Python Requests - POST request to change status

tushar saurabh November 12, 2018

I am using python requests to update the status of a Jira ticket. Below is the code - 

 

headers = {"Content-type": "application/json",'Accept' : 'application/json'}

body = {"update": {"comment": [{"add": {"body": "The ticket is resolved"}}]},"transition": {"id":id }}

url = f"http://jira.domain.com:8080/rest/api/2/issue/{key}/transitions"

r = requests.post(url,data=body,auth=HTTPBasicAuth('user', 'password'),headers=headers)

 

the value of key is correct. The value of transition id is correct. When I trigger the same post request through postman, Jira gets updated. However when executing it through python script, I get the following error - 

 

{"errorMessages":["Unexpected character ('u' (code 117)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@3bb9f13b; line: 1, column: 2]"]}

 

Please let me know, how to resolve the error.

2 answers

1 accepted

4 votes
Answer accepted
tushar saurabh November 13, 2018

The issue has been resolved. 

The request documentation says - 

Instead of encoding the dict yourself, you can also pass it directly using the json parameter (added in version 2.4.2) and it will be encoded automatically:

I changed the post request. instead of data=body, I made it json=body

 

 r = requests.post(url,json=body,auth=HTTPBasicAuth('XXX', 'YYY'),headers=headers)
Thienky Dang March 28, 2019

This solved my problem, thank you.

nayakiparithi October 16, 2019

The above hint solved my issue too :-)Thank you

0 votes
Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 13, 2018

Hi Tushar

I believe that you need a closing bracket and comma before comment, then remove the extra bracket before transition, so 

body = {"update":{}, "comment":[{ "add": { "body": "The ticket is resolved"}}],"transition": { "id": id  }}

is valid JSON and is a similar structure to what I use

tushar saurabh November 13, 2018

@Warren If I use your syntax, I am still getting the error. The error message is -

 

"Unexpected character ('c' (code 99)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@66c7db85; line: 1, column: 2]"

Suggest an answer

Log in or Sign up to answer