Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Assistance troubleshooting JIRA post request REST API (updating issue status)

Samuel Donadelli July 20, 2018

Hello community,

 

I am trying to update JIRA issue statuses by using a python script / Jira rest API post request. When I run the code there is an error 400 happening. Could someone help me debugging it?

 

Here is the code:

 

import requests
import json

# set the user name and password values
b64Val = 'xxxxxxxx'

# set the url and headers
url = "https://jira.xxxx.com/rest/api/2/issue/ISSUENUMBER-27/transitions?expand=transitions.fields"
header={"Authorization": "Basic %s" % b64Val, 'Content-Type': 'application/json'}

# simulating that the transition id will be status : TO DO
json = {"transitions": {"id": {1021}}}

#make the authenticated request to the JIRA API
r = requests.post(url, headers=header, data=json)

print ("HERE's the JSON RESPONSE\n\n ", r.status_code, r.headers)
print ("The response content is\n\n", r.content)

 

 

here is the error :

 

C:\Users\xxxxxx\PycharmProjects\xxxxxxx\xxxxxx>python test.py

("HERE's the JSON RESPONSE\n\n ", 400, {'X-ANODEID': 'ip-XX-X-XX-XXX.cloud.xxx.xxx.xxx', 'X-AUSERNAME': 'username', 'X-AREQUESTID': 'x58s6s5s6s5s1', 'X-Content-Type-Options': 'nosniff', 'Set-Cookie': 'JSESSIONID=3531C3F5E4dsdswe4444YR5B0FC5E5BFB;path=/;Secure;HttpOnly, atlassian.xsrf.token=BewweeH-3wewwP-VUG5-XCF2|252acbad9796ee1a9edb7bdectertertdfgf892f98dfe8f113|lin;path=/;Secure, AWSELB=DFA36D911ED9431079EA8A40E7B6sdfse324323423423532523809037333779072394E19436D8D7FA102AB26B5CCEC572A30E7146BA427A4007D7077583;PATH=/;SECURE;HTTPONLY', 'X-Seraph-LoginReason': 'OK', 'X-ASEN': 'SEN-104345443455', 'X-ASESSIONID': 'kk33434t', 'Content-Length': '193', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache, no-store, no-transform', 'Date': 'Fri, 20 Jul 2018 20:19:27 GMT', 'Content-Type': 'application/json;charset=UTF-8'}) ('The response content is\n\n', '{"errorMessages":["Unrecognized token \'transitions\': was expecting \'null\', \'true\', \'false\' or NaN\\n at [Source: org.apache.catalina.connector.CoyoteInputStream@2aa15555d347; line: 1, column: 13]"]}')

1 answer

1 accepted

2 votes
Answer accepted
Dave Chevell
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 20, 2018

Hey Samuel,

This one is an easy fix. Your json data is using the keyword transitions, when in fact what you should be using is transition; and you're putting the ID value in curly braces unnecessarily. Your json data should look like this:

{"transition": {"id": 1021}}

Whilst we’re at it though, I have a few tips to make writing your code easier.

The Requests library has a shortcut for doing Basic auth. Instead of manually crafting a Basic auth header, you can pass a tuple of your username and password to any requests call using the auth keyword argument, and the Basic auth header will be added for you:

auth = ('username', 'mypassword')
requests.post(url, auth=auth)

Requests also has an easier way of posting json. Instead of manually specifying the Content-type header, you can pass your data to your requests call using the json keyword argument, and the Content-type: application/json header will also be added for you.

data = {"transition": {"id": 1021}}
requests.post(url, auth=auth, json=data)

A third tip! For your URL, you don't need to include "?expand=transitions.fields" when you make a POST. This expand argument is only intended when retrieving transition data from the Jira API via a GET, to include additional information about available transitions. When it comes to actually making a POST, it's unnecessary. 

One last thing: in your example, you import the json library, but then you overwrite that namespace by assigning a dictionary to the variable also named json. This basically negates your import, as json now only refers to the new dictionary object you've created. As a general rule I wouldn't use json for variable names at all, precisely to avoid this kind of case. (As you can see, in my above example I used the variable name data instead.)

At the same time, none of your code seems to require the json library, and Requests has in built handling for both converting dicts to json data and converting json responses to dicts. So you don't need to import the json library anyway. 

Here's a complete version of the simplified code:

import requests

# set the user name and password values
auth = ('username', 'password')

# set the url
url = "https://jira.xxxx.com/rest/api/2/issue/ISSUENUMBER-27/transitions"

# simulating that the transition id will be status : TO DO
data = {"transition": {"id": 1021}}

#make the authenticated request to the JIRA API
r = requests.post(url, auth=auth, json=data)

print ("HERE's the JSON RESPONSE\n\n ", r.status_code, r.headers)

(I'm not printing the response content, because there is none. But sure enough, the issue status has changed)

Hope this helps Samuel!

Samuel Donadelli July 23, 2018

Thanks Dave, 

 

I changed the code as you said. 

 

Now the error is different , is an error 405.

405 Method Not Allowed Returned if the property key is not specified.

I don't know why. 

I will investigate on my side. If you have any insights it will be welcomed. Thanks a lot, Samuel 

Suggest an answer

Log in or Sign up to answer