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.
Just an update: The last code @Raghukrishna Hegde shared works fine but if you're using Python 3, you must change in the post request the attribute "data" for "JSON" as shown below:
r = requests.post(url, json=data, headers=header, auth=HTTPBasicAuth(user, passw))
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This documents the jira
python package (version 2.0.1.dev44), a Python library designed to ease the use of the JIRA REST API. Some basic support for the GreenHopper REST API also exis
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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."}}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This may be a permissions error. Does the account you're using have create privileges on the project?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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."}}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.