Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue with referencing local variable before declaration for API GET call

I’m having issue understanding how to return next_page token for my API call. It contains 100s of records and returns 30 at a time with next_page token to get the next 30… I’m able to get initial set of data and in my instance I’m just trying to return next_page tokens for the first 3 pages but so far it’s just returning the same 3.

def main_request(baseUrl, header, size):
    repeat_count = 3
    while repeat_count != 0:
        response = requests.get(baseUrl + f'?page_size={size}' + f"&next_page_token={page_token}", headers=header)
        api_data = response.json()
        page_token = api_data['next_page_token']
        print('Current Token: ', page_token)
        repeat_count -= 1

With above it doesn’t run because page_token hast not been declared but if I declare it at beginning and just set it to empty string, it runs but returns same 3 tokens from the first page.

JSON response if I set page_token = ”

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Current Token:  TY3fkmCPZJkI4PdufKrdxlC6cblJHKZnnJ2
Current Token:  TY3fkmCPZJkI4PdufKrdxlC6cblJHKZnnJ2
Current Token:  TY3fkmCPZJkI4PdufKrdxlC6cblJHKZnnJ2

>Solution :

Omit next_page from the payload to begin, then add it after each response.

def main_request(baseUrl, header, size):
    payload = {'page_size': size}
    for _ in range(3):
        response = requests.get(baseUrl, data=payload, headers=header)
        api_data = response.json()
        page_token = api_data['next_page_token']
        # Adds the first time, updates the others
        payload['next_page_token'] = page_token
        print('Current Token: ', page_token)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading