0

I need to loop my API call until I have grabbed all of the data that I need, since Jira only returns 50 at a time.

I want a loop that will continue this until the 'total' is less than the 'startAt' (I think)

I'm getting data back, but it doesn't appear to be looping to get ALL the data.

import json
import requests
import urllib3
import math


url = 'https://mydomain.atlassian.net/rest/api/2/search'

headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Basic 012336',
    'Postman-Token': "0123456789"
}

parameters = {
    'jql': 'project IN (A, B, C, D, E, F, G, H, I, J, K, L, M) AND issueType=incident AND statusCategory!=Done',
    'startAt': 0,
    'maxResults': 50,
    'fields': "key,status,project,priority,issuetype,created,statuscategory"
}

response = requests.request("GET", url, headers=headers, params=parameters)
jira_data = response.json()
jira_pretty = json.dumps(json.loads(response.text), sort_keys=True, indent=4)
int_total = jira_data.get('total')
int_maxResults = jira_data.get('maxResults')
int_startAt = parameters.get('startAt')
number_pages = math.ceil(int(int_total)/int(int_maxResults))

while int_startAt < int_total:
    int_startAt += int_maxResults
    if int_total <= int_startAt:
        break

print(jira_pretty)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Does this answer your question? [How to access the next page using JIRA -REST-API for python](https://stackoverflow.com/questions/33815378/how-to-access-the-next-page-using-jira-rest-api-for-python) – mkrieger1 Mar 05 '21 at 09:40

2 Answers2

2

Based on the code snippet provided, you should update the startAt parameter every loop before sending the next GET request. If you want to do fewer loops, you can set maxResults parameter to 100, which is the max value.

Stepping back a bit, it is not clear to me how you are looping through making multiple requests, at least based on the provided code.

Ian
  • 691
  • 3
  • 9
0

Here is my pagination

def jira_search(jql,fields):
        
        url = 'https://jira.domain.com/rest/api/2/search'


        data = []
        startAt = 0
        maxResults = 100
        total = 1


        while startAt <= total: 
                
               parameters = {
                'jql': jql,
                'startAt': startAt,
                'maxResults': maxResults,
                'fields': fields
                }

                headers = {
                'Content-Type': 'application/json',
                'Authorization': "Basic abcd"
                }

   
                print('Requesting jira data... ' + str(startAt) + ' from ' + str(total))
                response = requests.get(url, headers=headers, params=parameters)
                res = response.json()
                data += res['issues']
                startAt += maxResults
                total = res['total']

        return data
Jerry
  • 31
  • 1
  • 7
  • [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – Muhammad Mohsin Khan Apr 06 '22 at 12:04