Trying to use Python & REST API to create an issue in Jira Cloud. Getting response 400, which is "Bad Request." How to tell what's bad? Is it the headers? The payload? Did I mess up the token or base64 steps? Also, I see python examples on the site that don't use the same headers. The first link below is current, but I'm not sure the Python example code is up to date.
https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-post
https://community.atlassian.com/forums/Jira-questions/how-to-login-jira-cloud-with-python-rest-api-without-importing/qaq-p/1070920
Thank you for any assistance.
Hi @Dave Puryear, welcome to the community.
According to the Atlassian documentation, a 400 status code means "the request does not have the required fields, or the fields the request has are invalid in some way."
In my experience, when I get a 400 status code response, the JSON document request body is either syntactically or semantically malformed. There are JSON validators online to check the syntax and structure of your request document.
Checking the request semantics, like required fields, start by looking a the response body of the error message. Some companies put error details in the body, I don't remember if Atlassian does or not.
Thank you, Jim. JSON is solid. No error details, other than "Bad Request." Curious if anyone can connect to their Jira cloud using the snippet provided at https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-post:
# This code sample uses the 'requests' library: # http://docs.python-requests.org import requests from requests.auth import HTTPBasicAuth import json url = "https://your-domain.atlassian.net/rest/api/3/issue" auth = HTTPBasicAuth("email@example.com", "<api_token>") headers = { "Accept": "application/json", "Content-Type": "application/json" } payload = json.dumps( { "fields": { "assignee": { "id": "5b109f2e9729b51b54dc274d" }, "components": [ { "id": "10000" } ], "customfield_10000": "09/Jun/19", "customfield_20000": "06/Jul/19 3:25 PM", "customfield_30000": [ "10000", "10002" ], "customfield_40000": { "content": [ { "content": [ { "text": "Occurs on all orders", "type": "text" } ], "type": "paragraph" } ], "type": "doc", "version": 1 }, "customfield_50000": { "content": [ { "content": [ { "text": "Could impact day-to-day work.", "type": "text" } ], "type": "paragraph" } ], "type": "doc", "version": 1 }, "customfield_60000": "jira-software-users", "customfield_70000": [ "jira-administrators", "jira-software-users" ], "customfield_80000": { "value": "red" }, "description": { "content": [ { "content": [ { "text": "Order entry fails when selecting supplier.", "type": "text" } ], "type": "paragraph" } ], "type": "doc", "version": 1 }, "duedate": "2019-05-11", "environment": { "content": [ { "content": [ { "text": "UAT", "type": "text" } ], "type": "paragraph" } ], "type": "doc", "version": 1 }, "fixVersions": [ { "id": "10001" } ], "issuetype": { "id": "10000" }, "labels": [ "bugfix", "blitz_test" ], "parent": { "key": "PROJ-123" }, "priority": { "id": "20000" }, "project": { "id": "10000" }, "reporter": { "id": "5b10a2844c20165700ede21g" }, "security": { "id": "10000" }, "summary": "Main order flow broken", "timetracking": { "originalEstimate": "10", "remainingEstimate": "5" }, "versions": [ { "id": "10000" } ] }, "update": {} } ) response = requests.request( "POST", url, data=payload, headers=headers, auth=auth ) print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
I'm curious about the date format on the customfield_10000 and customfield_20000 attributes.
The API docs show two formats (one for dates, one for datetime) that include four-digit years separated by dashes. If those custom fields are just text, it might work, but if they're date or datetime fields, I could see it being a problem.
ref: https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/#datepickerfield
The Create Issue example I linked to is far more complicated than it needs to be. Let's ignore all the custom fields for now. My test code only uses the minimum "summary" "project name" and "task type." Most examples I find (from the last few years) show base64 encoding of the api token. The api example I linked to does not. Various questions asked over the years are a mix of old and new methods, headers, etc., none of which I can get to work.
Here's where I'm at now:
auth_hdr = f'Basic {b64_auth}'headers = {"Authorization": auth_hdr,"Content Type": "application/json"}payload = json.dumps( {"fields": { "issuetype": { "id": issue_type_id }, "project": { "name": project_name }, "summary": summary},"update": {}} )print(f'headers = {headers}\n')print(f'payload = {payload}\n')response = requests.request( "POST", url, data=payload, headers=headers)print(response)
It looks like you're new here. Sign in or register to get started.