Can someone help me with POST request to JIRA using the Python

Hi All,

     I was going through the article "Writing a REST Client in python". I wanted to modify it to create issues in JIRA. I have already tested the POST API of JIRA  using the curl command line and it works fine.

curl -D- -u <username>:<passwd> -X POST --data @data.txt -H "Content-Type: application/json" https://<jira-url>

So i modified the python code mentioned in the article  "Writing a REST Client in python". I am using python2.7. The GET API works fine as mentioned in the article. Also i am using the  python-rest-client package.

The code changes for the POST is as follows:

jiraF=open("/root/data.txt","r")
jira_input=json.loads(jiraF.read())
jira_input_raw=json.dumps(jira_input)

resp = conn.request_post("/issue/", body=jira_input_raw, headers={'content-type':'application/json', 'accept':'application/json'})

When i execute the command

python2.7 jira-create.py

I get the following error message from JIRA site

The status message is 400
The JSON response output is {"errorMessages":[],"errors":{"summary":"Field 'summary' cannot be set. It is not on the appropriate screen, or unknown.","components":"Field 'components' cannot be set. It is not on the appropriate screen, or unknown.","versions":"Field 'versions' cannot be set. It is not on the appropriate screen, or unknown.","customfield_18130":"Field 'customfield_18130' cannot be set. It is not on the appropriate screen, or unknown.","customfield_13232":"Field 'customfield_13232' cannot be set. It is not on the appropriate screen, or unknown.","description":"Field 'description' cannot be set. It is not on the appropriate screen, or unknown.","reporter":"Field 'reporter' cannot be set. It is not on the appropriate screen, or unknown.","customfield_19030":"Field 'customfield_19030' cannot be set. It is not on the appropriate screen, or unknown."}}

Has anyone implemented creating issue in JIRA using  python-rest-client package which is mentioned in this article. Or else can anyone tell me what could be the problem here. 

 

 

2 answers

0 vote
Josh Steckler Community Champion Aug 02, 2017

This may be a permissions error. Does the account you're using have create privileges on the project?

Yes i do have create previlages. I tested this out using the curl command and i am able to create the issues in JIRA. I used the following command

curl -D- -u <username>:<passwd> -X POST --data @data.txt -H "Content-Type: application/json" https://<jira-url>

This works fine. But as i mentioned above when i try using python, i get the error mentioned above. As i mentioned above, i used the article 

https://developer.atlassian.com/fecrudev/integration-tutorials/writing-a-rest-client-in-python

as my reference. In this article only GET operations are discussed. I slightly modified the same for POST operations i am getting that error. I am not sure if i am doing something wrong here. Can you please help?

Josh Steckler Community Champion Aug 02, 2017

Are you sure you're using the same credentials in curl and python? In your code above I don't see the username/password.

It's the "summary cannot be set" that is making me think this. Summary pretty much has to be on the Create screen.

Hi Josh,

     Username/password is fine. The same username/password works in python for the get operation. Also the same username/password works fine when using POST operation through curl command. Only when i am using POST operation through the python i am getting that error. I am not sure am i making some mistake in sending the data through the 

resp = conn.request_post("/issue/", body=jira_input_raw, headers={'content-type':'application/json', 'accept':'application/json'})

Let me put the whole code for your reference:

import json
# import the REST library
from restful_lib import Connection

base_url = "https://<the_jira-url>"

conn = Connection(base_url, username="use_name", password="passwd")

jiraF=open("/root/data.txt","r")


jira_load=jiraF.read()
print 'The json data just after read' jira_load

jira_input=json.loads(jiraF.read())
print 'The json input is', jira_input

jira_input_raw=json.dumps(jira_input)
print 'The json dump of jira is', jira_input_raw

resp = conn.request_post("/issue/", body=jira_input_raw, headers={'content-type':'application/json', 'accept':'application/json'})

status = resp[u'headers']['status']
print 'The status message is', status
print 'The JSON response output is', resp[u'body']

======================================

 

The output of this is follwoing:

root@radiusServer:~/auto-jira# python2.7 jira-create.py
The json data just after read {
"fields": {
"project":
{
"key": "NSLCM"
},
"summary": "svcacc_krish second issue creation",
"description": "This is the second issue created using the JIRA REST API for account krish",
"issuetype": {
"name": "Bug"
},
"components": [
{
"name": "NS-OTHER"
}
],
"customfield_13232": {
"value": "Internal - Automation"
},
"customfield_18130": {
"value": "S4"
},
"reporter": {
"name": "raghukrish1"
},
"customfield_19030": {
"value": "No - Existed Previously"
},
"versions": [
{
"name": "10.1"
}
]
}
}

 

The json input is {u'fields': {u'description': u'This is the second issue created using the JIRA REST API for account krish', u'reporter': {u'name': u'raghukrish1'}, u'versions': [{u'name': u'10.1'}], u'summary': u'svcacc_krish second issue creation', u'project': {u'key': u'NSLCM'}, u'customfield_13232': {u'value': u'Internal - Automation'}, u'customfield_19030': {u'value': u'No - Existed Previously'}, u'components': [{u'name': u'NS-OTHER'}], u'issuetype': {u'name': u'Bug'}, u'customfield_18130': {u'value': u'S4'}}}

 

The json dump of jira is {"fields": {"description": "This is the second issue created using the JIRA REST API for account krish", "reporter": {"name": "raghukrish1"}, "versions": [{"name": "10.1"}], "summary": "svcacc_krish second issue creation", "project": {"key": "NSLCM"}, "customfield_13232": {"value": "Internal - Automation"}, "customfield_19030": {"value": "No - Existed Previously"}, "components": [{"name": "NS-OTHER"}], "issuetype": {"name": "Bug"}, "customfield_18130": {"value": "S4"}}}

 

The status message is 400

The JSON response output is {"errorMessages":[],"errors":{"summary":"Field 'summary' cannot be set. It is not on the appropriate screen, or unknown.","components":"Field 'components' cannot be set. It is not on the appropriate screen, or unknown.","versions":"Field 'versions' cannot be set. It is not on the appropriate screen, or unknown.","customfield_18130":"Field 'customfield_18130' cannot be set. It is not on the appropriate screen, or unknown.","customfield_13232":"Field 'customfield_13232' cannot be set. It is not on the appropriate screen, or unknown.","description":"Field 'description' cannot be set. It is not on the appropriate screen, or unknown.","reporter":"Field 'reporter' cannot be set. It is not on the appropriate screen, or unknown.","customfield_19030":"Field 'customfield_19030' cannot be set. It is not on the appropriate screen, or unknown."}}

Josh Steckler Community Champion Aug 02, 2017

It could possibly a problem with the rest client package being used. This has to be some kind of authentication problem since it's saying summary can't be set.  The REST api frequently gives errors that are not what they seem.

Thanks Josh. Let me check for that angle.

0 vote

The response you are getting tells you what the problem is - you are giving it a load of field data for fields that are not there in your system

You should check what fields you do get by using the UI to create a similar issue.

Thanks for your response Nic. But i am able to do the create operation successfully with the exact same data using curl command. If there was some field i should not have given, then i would have got that error when executing the POST opeartion through the curl command as well.

curl -D- -u <username>:<passwd> -X POST --data @data.txt -H "Content-Type: application/json" https://<jira-url>

This works. The data.txt is the same file i use to read in python. The code snippet is 

import json
# import the REST library
from restful_lib import Connection

base_url = "https://<the_jira-url>"

conn = Connection(base_url, username="use_name", password="passwd")

jiraF=open("/root/data.txt","r")


jira_load=jiraF.read()
print 'The json data just after read' jira_load

jira_input=json.loads(jiraF.read())
print 'The json input is', jira_input

jira_input_raw=json.dumps(jira_input)
print 'The json dump of jira is', jira_input_raw

resp = conn.request_post("/issue/", body=jira_input_raw, headers={'content-type':'application/json', 'accept':'application/json'})

status = resp[u'headers']['status']
print 'The status message is', status
print 'The JSON response output is', resp[u'body']

The output of this is follwoing:

root@radiusServer:~/auto-jira# python2.7 jira-create.py
The json data just after read {
"fields": {
"project":
{
"key": "NSLCM"
},
"summary": "svcacc_krish second issue creation",
"description": "This is the second issue created using the JIRA REST API for account krish",
"issuetype": {
"name": "Bug"
},
"components": [
{
"name": "NS-OTHER"
}
],
"customfield_13232": {
"value": "Internal - Automation"
},
"customfield_18130": {
"value": "S4"
},
"reporter": {
"name": "raghukrish1"
},
"customfield_19030": {
"value": "No - Existed Previously"
},
"versions": [
{
"name": "10.1"
}
]
}
}

 

The json input is {u'fields': {u'description': u'This is the second issue created using the JIRA REST API for account krish', u'reporter': {u'name': u'raghukrish1'}, u'versions': [{u'name': u'10.1'}], u'summary': u'svcacc_krish second issue creation', u'project': {u'key': u'NSLCM'}, u'customfield_13232': {u'value': u'Internal - Automation'}, u'customfield_19030': {u'value': u'No - Existed Previously'}, u'components': [{u'name': u'NS-OTHER'}], u'issuetype': {u'name': u'Bug'}, u'customfield_18130': {u'value': u'S4'}}}

 

The json dump of jira is {"fields": {"description": "This is the second issue created using the JIRA REST API for account krish", "reporter": {"name": "raghukrish1"}, "versions": [{"name": "10.1"}], "summary": "svcacc_krish second issue creation", "project": {"key": "NSLCM"}, "customfield_13232": {"value": "Internal - Automation"}, "customfield_19030": {"value": "No - Existed Previously"}, "components": [{"name": "NS-OTHER"}], "issuetype": {"name": "Bug"}, "customfield_18130": {"value": "S4"}}}

 

The status message is 400

The JSON response output is {"errorMessages":[],"errors":{"summary":"Field 'summary' cannot be set. It is not on the appropriate screen, or unknown.","components":"Field 'components' cannot be set. It is not on the appropriate screen, or unknown.","versions":"Field 'versions' cannot be set. It is not on the appropriate screen, or unknown.","customfield_18130":"Field 'customfield_18130' cannot be set. It is not on the appropriate screen, or unknown.","customfield_13232":"Field 'customfield_13232' cannot be set. It is not on the appropriate screen, or unknown.","description":"Field 'description' cannot be set. It is not on the appropriate screen, or unknown.","reporter":"Field 'reporter' cannot be set. It is not on the appropriate screen, or unknown.","customfield_19030":"Field 'customfield_19030' cannot be set. It is not on the appropriate screen, or unknown."}}

Ok, that means that your python can't be logging in correctly, as it's not able to "see" the create screen for that project.

I've not used Python to do this sort of stuff, so I'm stuck.  The code looks ok to me, but obviously the connection thing isn't being set correctly.

Thanks Nic. Let me check from that point as well

Hi Nic and Josh,

    I finally managed to create the JIRA issues using the python script. I got rid of the python-rest-client package, and programmed using the response library. It turned out to be much simpler. I will post the code here for comunitie's benefit.

import json
import requests
from requests.auth import HTTPBasicAuth


url = "https://<jira-url>/rest/api/2/issue"
header = {'Content-Type': 'application/json'}
#set the user name and password values
user = <username>

# block for setting the JIRA password
password = <passwd>

#setting the file from which to read the JSON which will be passed into the JIRA API
jiraF=open("/root/data.txt","r")
jira_load=jiraF.read()
#make the authenticated request to the JIRA API
r = requests.post(url, data=jira_load, headers=header, auth=HTTPBasicAuth(user, password))
print ("HERE's the JSON RESPONSE\n\n ", r.status_code, r.headers)
print ("The response content is\n\n", r.content)

 

To highlight my problem, there were two issues. One is not using the HTTPBasicAuth method while usernae and passwd to the response.post method, and the other is some typo mistake in the header = {'Content-Type': 'application/json'}. Instead of 'Content-Type', i initially typed 'Content Type'(no hyphen in between). This used to give me 415 http error code.

I think the original issue with using rest-client package was, as you earlier mentioned might be because of authentication problem. So to try it out i gave some wrong username and passwd in the GET request as well, and still i was able to get the proper response. It could be that i was not checking the authentication for GET requests.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

275 views 3 11
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you