Forums

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

Using REST API to create JIRA ticket - issue with some field inputs

Robin Dublon November 7, 2024

I am trying to using the JIRA REST API to create a ticket (want to incorporate it into an external function to feed to JIRA), specifically through Python (though will use another method if you advise that will work better). One of the example issues I get is that custom_field13500 (Organizations) doesn't accept the input I have as correct, and wants it in an array, however this is an exact copy of an existing tickets value so it is odd I am getting this answer. See the code below and let me know what we need to change or if you have a better way of doing this:

import requests
from requests.auth import HTTPBasicAuth
import json

url = "https://project_domain.atlassian.net/rest/api/3/issue"

auth = HTTPBasicAuth("<email_address>", "<API_Token>")

headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}

payload = json.dumps({
"fields": {
"project": {
"id": "21489"
},
"summary": "TEST Ticket via API with certain fields",
"issuetype": {
"id": "11152"
},
"priority": {
"id": "3"
},
"assignee": {
"id": "62d536279189e98a2017cf94"
},
"description": {
"type": "doc",
"version": 1,
"content": [
{
"type": "paragraph",
"content": [
{
"text": "This is a test ticket created via API.",
"type": "text"
}
]
}
]
},
"customfield_13500": [
{
"id": "122",
"name": "RJ Test Organisation Ltd",
"created": {
"iso8601": "2024-10-28T17:23:22+0000",
"jira": "2024-10-28T17:23:22.928+0000",
"friendly": "28/Oct/24 5:23 PM",
"epochMillis": 1730136202928
},
"_links": {
"self": "https://project_domain.atlassian.net/rest/servicedeskapi/organization/122"
}
}
],
"customfield_14601": {
"id": "12201"
},
"customfield_14605": {
"self": "https://project_domain.atlassian.net/rest/api/3/customFieldOption/12278",
"value": "More info required",
"id": "12278"
},
"timetracking": {
"originalEstimate": "10",
"remainingEstimate": "5"
},
"status": {
"id": "10005"
},
"labels": ["API", "test_creation"]
}
})

response = requests.post(url, headers=headers, data=payload, auth=auth)

print(response.status_code)
print(response.json())

response = requests.request(
"POST",
url,
data=payload,
headers=headers,
auth=auth
)

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

 

2 answers

1 accepted

0 votes
Answer accepted
Robin Dublon November 14, 2024

We found the solution via using a request api:

/rest/servicedeskapi/request

Then in the same script we take the issueKey Id and use that to then call an issue specific api:

/rest/api/3/issue/<Ticket_ID>

This will update the ticket with the organisation and any other values we need (can either be hardcoded or be values that can be declared before the script runs).

0 votes
Jim Knepley - ReleaseTEAM
Atlassian Partner
November 7, 2024

Welcome to the community, @Robin Dublon 

I don't recognize the structure of the JSON you're sending for customfield_13500, what is that field's type? I suspect that you're sending more data than is necessary.

Some random notes about your use of the requests library:

In the request, instead of "data" you use the "json" parameter, you can attach a dictionary and the library will take care of JSON encoding, setting the content-type, etc. This way you don't have to import the json library yourself. There is a corresponding json method on response objects.

I like to use requests session objects when I'm setting authentication headers. It's more important as your code gets more complex, but it removes some boilerplate code.

Robin Dublon November 7, 2024

The field responds that it is requiring an array (perhaps due to it allowing multiple values?), and this "customfield_13500" is actually the Organizations field, not sure why it is listed as a custom field.

For the attaching of a dictionary to take part of the JSON coding, are there any examples of this online I could use? As I have now removed the json.dumps aspect of the code, but that's probably not what you mean.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events