Iterating through Jira API Url

Viewed 273

I'm having issues iterating through the Jira API. My goal is to iterate until I extract the response['total']which is 7,000 records. My maxResults is 1,000 per request.

I'm getting a successful response (status code= 200) and my correct 1,000 on maxResults but I'm not updating my startAt, which starts at 0, but I'm expecting to add up to the maxResults. My goal is to iterate through the Jira API to extract all the 7,000 records and append them together as dataframes.

enter image description here

baseUrl= https://example.com/rest/api/2/search?jql=issuetype='Story'
total = 1
maxResults = 0
startAt = 0

while (startAt + maxResults) < total:
            data = []
            response = requests.get((baseUrl+'&startAt='+str(startAt + maxResults)), auth = (username, password))
            print(response.status_code)
            r = response.json()
            jiraIssues = r["issues"]

          
            for issue in issues:

                results_1= json.dumps((issues), sort_keys= True,indent = 4)

                results_2 =json.loads(results_1)

                results_3 = pd.json_normalize(results_2)

                data.append(results_3)

      
                print (len(issues))

                print (startAt)

                print("----------")

                maxResults = response['maxResults']

                total = response['total']

                startAt = response['startAt']

            else:

                break

                print("none")
1 Answers

In order to iterate through the API results, you need to be incrementing your startAt parameter by adding maxResults to it at the end of every while loop iteration:

startAt = startAt + maxResults

The startAt parameter indicates the result number to begin at when retrieving results from your API call -- think of it like a "page" parameter. Each successive API call must request the next "page" of results in order to iterate through and reach the end of the results.

Related