Sep-01-2022, 03:02 AM
(This post was last modified: Sep-01-2022, 03:03 AM by shantanu97.)
I am having trouble writing pagination code in Python
How does the API request work?
First call by the client will be the regular endpoint of the API. The response from the call contains pagination metadata for the subsequent calls. The API limits the request to 100 records, and if the endpoint has more, then I would have to paginate, and used "Next Page Link" from the response to make subsequent calls. The "NextPageLink" would be null on the last page, where the client would end the call.
You can check the basic structure for the API response here in the API Doc
I just want to paginate API and get all data inside the csv file.
When I run the Python code it doesn't bring any data, it just brings headers. Also, I am not sure my API looping is working correctly or not.
Any help what I am doing wrong in the code.
How does the API request work?
First call by the client will be the regular endpoint of the API. The response from the call contains pagination metadata for the subsequent calls. The API limits the request to 100 records, and if the endpoint has more, then I would have to paginate, and used "Next Page Link" from the response to make subsequent calls. The "NextPageLink" would be null on the last page, where the client would end the call.
You can check the basic structure for the API response here in the API Doc
I just want to paginate API and get all data inside the csv file.
When I run the Python code it doesn't bring any data, it just brings headers. Also, I am not sure my API looping is working correctly or not.
Any help what I am doing wrong in the code.
import requests import csv import pandas as pd results = [] # empty list csvheader = [] headers = {} params = {} url = "https://api-peopletray.azurewebsites.net/api/reporting/2321B899-0907-4D2F-9BDD-66D924E699DD/checklists" try: while True: response = requests.get(url, data=params, headers=headers) # print(response.status_code) # for debug # print(response.text) # for debug json_data = response.json() results += json_data['data'] if "nextPageLink" not in json_data['nextPageLink']: break url = json_data['nextPageLink'] except Exception as ex: print("Exception:", ex) with open('APIData.csv','w',encoding='UTF8', newline='')as f: writer = csv.writer(f) writer.writerow(csvheader) writer.writerows(results)APIData.csv file attached.
Attached Files