How to pass jql when calling search REST API?

李冲 August 23, 2018

I have one request to get all the unclosed tickets under one project and close them. I googled and found some useful links in atlassian.  How-to-get-more-than-50-issues-with-search-API 

The call looks like this:

rest/api/2/search?jql=SOME_JQL&fields=summary,duedate,issuetype&startAt=51

If I enter below in the JIRA board.

捕获.PNG

I should replace the AND with & when calling the search API right? If there any other character I need to escape also? And If I want to get all the tickets back, what is the best way to do? I tried with below ones, the performance is very slow and it is not a good way to set the maxResults to a static value.

rest/api/2/search?jql=TestProject&resolution=Unresolved ORDER BY priority DESC, updated DESC&maxResults=5000 

 

Thanks in advance!

3 answers

1 accepted

6 votes
Answer accepted
Mauricio Karas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 24, 2018

Hi, Christina.

The search rest call "/rest/api/2/search" can be also used as a POST request, which I recommend in your case. If you send it as a POST you can specify the JQL in a JSON payload and you don't need to escape any character, also this method is recommended if the JQL it's too long for the URL.

You can also specify in the payload what fields you want it to return, like so:

 "fields": ["summary","assignee","status"]

This could improve the performance on the request. I also recommend doing the calls in batches instead of requesting 5000 users.

Kind regards,
Maurício Karas

李冲 August 26, 2018

Thanks so much Karas. I tried with what you said and now the performance improves a lot. :)

 

But I still met one issue. Under my project, there are nearly 10 thousand tickets. If i am calling like this:

POST: /rest/api/2/search

body: 

{

jql: "project = TestProject And resolution = Unresolved",

maxResults: 1000,

startAt:1000

}

 

The API throws 500 error back. If I set the startAt as 0, then it works fine. Do you know why this occurs? Thanks a lot!

李冲 August 26, 2018

Actually, I don't quite understand what the document says about startAt: 

  • startAt is the index of the first item returned in the page of results.
  • maxResults is the maximum number of items that can be returned per page. Each API endpoint may have a different limit for the number of items returned, and these limits may change without notice.
  • total (optional) is the total number of items contained in all pages. This number may change as the client requests the subsequent pages, therefore the client should always assume that the requested page can be empty.
  • isLast (optional) indicates whether the page returned is the last one.

 

How can I know how many pages my project has? I tried several different startAt, the value returns confused me. Sometimes it succeeded and sometimes it threw 500 error. I would like to get all the tickets and close them. For my case, what startAt value should I set for each call? Thanks.

Francisco Morgado Gonzalez May 7, 2020

Thanks a lot. 

An CURL example. It's works for me:

 

curl -N -u "admin:admin" -X POST -H "Accept-Encoding: gzip,deflate" -H "Content-type: application/json" --data '{"jql":"project = PROYECKEY AND (issue in allFromEpic('PROYECKEY-1') OR issue in allFromEpic('PROYECKEY-2')) AND updatedDate >= 2020-04-01 and updatedDate < 2020-05-01","startAt":0,"maxResults":10,"fields":["key","issuetype","summary","priority","status","created","updated"]}' "http://localhost:8080/rest/api/2/search"

Like Sid likes this
0 votes
tom_engels March 15, 2024

@Mauricio Karas I found this to be a very helpful thread, thank you! I want to pay it forward by offering another GET request example.

Note: My Python function is nested within a class containing other Jira API interactions (other aspects of the class not included) and I'm on Mac OS so authentication technique may look different for other people.

import requests
import os
JIRA_AUTH_TOKEN = os.environ.get('JIRA_AUTH_TOKEN')

headers = {
"Authorization": f"Bearer: {JIRA_AUTH_TOKEN}",
}
def get_issues_keys_in_version(self, project_key: str, version_name: str) -> list:
get_parameters = {
'jql': f"project = {project_key} AND fixVersion = {version_name}",
}
generic_endpoint = "https://jira.yourcompanydomain.com/rest/api/2/search"
response = requests.get(url=generic_endpoint, headers=headers, params=get_parameters)
response.raise_for_status()
print(response.url)
data = response.json()
issue_keys = [issue['key'] for issue in data['issues']]
return issue_keys
0 votes
李冲 August 28, 2018

I searched and did some tests and now understand. thanks fro your help Karas.

Suganya July 18, 2023

Hi

Could you please lt me know how to set maxResults to get all the project details or setting it to a constant value like 5000 or something?

Suggest an answer

Log in or Sign up to answer