400 error when creating test issue using API

srinivas nayani April 4, 2017

I am trying to create a test issue using REST API. I am doing in python.

However I am getting 400 error saying bad request.Can you please correct if

something is incorrect.

Here is the python code.

 

rom urllib2 import Request, urlopen
from json import dumps, load
from base64 import b64encode
def createIssue():
# Variables used:
# 'username' and 'password' variables for encoding into header below
username = 'snayani@threatmetrix.com'
password = 'test'
# New Test Issue Variables:
issueProjectKey = "QA"
issueSummary = "Login Service Test"
issueDescription = "Login Credential Combination Test"
issueTypeName = "Test"
# 'baseURL' holds basic data for JIRA server that will be used in API calls
# 'createTestIssueURL' hold URL sent to JIRA to create a new Issue
# 'createTestStepURL' hold URL sent to ZAPI to add a test step to a Test issue
baseURL = 'https://jira.sac.int.threatmetrix.com'
createTestIssueURL = baseURL + "/rest/api/2/issue/"
# 'headers' holds information to be sent with the JSON data set
# Initialized with Auth and Content-Type data
# Authorization header uses base64 encoded username and password string
headers = {"Authorization": " Basic " + b64encode(username + ":" + password), "Content-Type": "application/json"}

# ///// Create Test Issue /////
# Create a new Zephyr for JIRA Test issue in specified project

print "Creating Test Issue..."

newTestValues = dumps({
"fields": {
"project": {
"key": issueProjectKey
},
"summary": issueSummary,
"description": issueDescription,
"issuetype": {
"name": issueTypeName
}
}
})
request = Request(createTestIssueURL, data=newTestValues, headers=headers)
js_res = urlopen(request)
newTestId = objResponse['id']

print "Test Issue Created! New ID is: " + newTestId


if __name__ == "__main__":
createIssue()

6 answers

0 votes
srinivas nayani April 6, 2017

thanks.it helped

0 votes
srinivas nayani April 5, 2017

Thanks for your quick help on  questions. It worked using the id. I have following question.

How can get the id of the new new test case that was created.

what would i need to do after this call. I looked at api was not able to find one?

new_issue = jira.create_issue(project='10035', summary='New issue from jira-python',description='Look into this one', issuetype={'id':'10601'})

Ron Gates April 5, 2017

The key of newly created issue (in form of "QA-1234" string) is stored in "new_issue" variable.

Use "print" at the end of your script to see what that variable is holding - for Python 2 you can do it like that:

print new_issue

Then to display Issue ID instead of Issue Key use this:

print new_issue.id
0 votes
srinivas nayani April 4, 2017

I have tried to create issue using the following code. How ever i get error saying issueType is invalid. I have given the issueType as "Test" and i confirmed it is valid issue type. Here is the code.

Please Suggest.

 

from jira import JIRA

username = 'snayani@threatmetrix.com'
password = 'test123'
jira_instance='https://jira.sac.int.threatmetrix.com'

 

# Authenticating
jira = JIRA(jira_instance, basic_auth=(username, password))
print "auth Successful"

new_issue = jira.create_issue(project='QA', summary='New issue from jira-python',
description='Look into this one', issuetype={'name': 'Test'})

print "create sucess"

 

ERROR

--------

auth Successful
Traceback (most recent call last):
File "test2.py", line 14, in <module>
description='Look into this one', issuetype={'name': 'Test'})
File "C:\Python27\lib\site-packages\jira\client.py", line 899, in create_issue
r = self._session.post(url, data=json.dumps(data))
File "C:\Python27\lib\site-packages\jira\resilientsession.py", line 153, in post
return self.__verb('POST', url, **kwargs)
File "C:\Python27\lib\site-packages\jira\resilientsession.py", line 146, in __verb
raise_on_error(response, verb=verb, **kwargs)
File "C:\Python27\lib\site-packages\jira\resilientsession.py", line 56, in raise_on_error
r.status_code, error, r.url, request=request, response=r, **kwargs)
jira.exceptions.JIRAError: JiraError HTTP 400 url: https://jira.sac.int.threatmetrix.com/rest/api/2/issue
text: The issue type selected is invalid.

response headers = {'X-AUSERNAME': 'snayani%40threatmetrix.com', 'X-AREQUESTID': '1359x6280742x2', 'X-Content-Type-Options': 'nosniff', 'Transfer-Encoding': 'chunked', 'X-Seraph-LoginReason': 'OK', 'X-ASEN': 'SEN-3045975', 'X-ASESSIONID': '13potwd', 'Connection': 'close', 'Cache-Control': 'no-cache, no-store, no-transform', 'Date': 'Tue, 04 Apr 2017 22:39:13 GMT', 'Server': 'Apache-Coyote/1.1', 'Content-Type': 'application/json;charset=UTF-8'}
response text = {"errorMessages":[],"errors":{"issuetype":"The issue type selected is invalid."}}

Ron Gates April 4, 2017

The error in response header tells you exactly what is causing the issue: "The issue type selected is invalid" - this means you have to adjust the "issuetype" in the code to something that actually exists in your Jira instance.


I don't know how your Jira is configured but from the error looks like "QA" Project exists in it but under that "QA" Project there is no "Test" Issue Type defined.

srinivas nayani April 4, 2017

please look at the issueTypes in the image below. There is issue type called "Test" in "QA" project. Please check if something is wrong..

thanks

test.png

Ron Gates April 5, 2017

Instead of operating on names I recommend to operate on IDs then.

You can easily extract IDs utilizing your web browser, just go to:

https://jira.sac.int.threatmetrix.com/rest/api/2/issue/QA-2762

This will display JSON blob for that particular QA issue. In that blob you can find project id (just search for "project" - id number is in that section) and issue type id (just search for "issuetype" - id number is in that section)

Then adjust your python code to:

new_issue = jira.create_issue(project='project_id', summary='New issue from jira-python', description='Look into this one', issuetype={'id': 'issuetype_id'})

I tested this approach on my Jira instance and it works fine for me.

0 votes
srinivas nayani April 4, 2017

thanks..will try

0 votes
srinivas nayani April 4, 2017

Thanks for reply. 

# The issue key doesn't really matter - it just has to be an existing JIRA ticket
jira_issue = 'JIRA-1234'

 

Do we have to to create a jira-issue before ? I am thinking api wiill create a issue and will return the id back?

 

I am trying to create a new test issue  using POST by  passing  testname and description. Can you please show how to do that or what is wrong in my code?

Ron Gates April 4, 2017

The example I gave is just showing how to use the library to get you started.

If you want to see example of issue creation please refer to the official Jira Python library documentation:

https://jira.readthedocs.io/en/master/examples.html#issues

(TIP: Look for "Creating issues is easy:" paragraph).

0 votes
Ron Gates April 4, 2017

I recommend using Jira Python library that simplifies Jira REST API operations from Python.

Here is an example for Python 3 (but library also works with Python 2):

# This script is getting all options for Teams field from JIRA issue

from jira import JIRA
import getpass

username = input('Jira username: ')
password = getpass.getpass(prompt='Jira password: ')

jira_instance = 'https://yourjirainstance.com'

# The issue key doesn't really matter for getting all options for Teams field - it just has to be an existing JIRA ticket
jira_issue = 'JIRA-1234'

# Authenticating
jira = JIRA(jira_instance, basic_auth=(username, password))

# Getting meta data for the issue
issues_meta = jira.editmeta(jira_issue)

# Will save file in the folder that you are running script from
file = open('output.txt', 'w')

# customfield_11000 is "Teams" field
for field in issues_meta['fields']['customfield_11000']['allowedValues']:
 print(field['value'])
 file.write(field['value'] + '\n')

file.close()

Also documentation that I linked provides a lot of good examples.

Good luck and let us know if this info helped!

Suggest an answer

Log in or Sign up to answer