title: How to run a while loop to run a REST API call until no more results come back in Python
body: <p>I'm writing a short Python program to request a JSON file using a Rest API call.  The API limits me to a relatively small results set (50 or so) and I need to retrieve several thousand result sets.  I've implemented a while loop to achieve this and it's working fairly well but I can't figure out the logic for 'continuing the while loop' until there are no more results to retrieve.  Right now I've implemented a hard number value but would like to replace it with a conditional that stops the loop if no more results come back.  The 'offset' field is the parameter that the API forces you to use to specify which set of results you want in your 50.  My logic looks something like...</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>import requests
  import json
  from time import sleep
  
  url = "https://someurl"
  offsetValue = 0
  
  PARAMS = {'limit':50, 'offset':offsetValue}
  
  
  
  headers = {
     "Accept": "application/json"
  }
  
  while offsetValue &lt;= 1000:
     response = requests.request(
        "GET",
         url,
        headers=headers,
        params = PARAMS
     )
     testfile =  open("testfile.txt", "a")
     testfile.write(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
     testfile.close()
     offsetValue = offsetValue + 1
     sleep(1)</code></pre>
  </div>
  </div>
  </p>
  <p>So I want to change the conditional the controls the while loop from a fixed number to a check to see if the result set for the getRequest is empty.  Hopefully this makes sense.</p>