1+ GET request returning error 403 to heroku python
<p>I'm using an api in my script. When I run the script via my own terminal, I'm successfully able to make 3 calls to the endpoint. But when I run the same script on heroku bash, only the first call is success, other two return Error 403. Here's my code</p>

<pre><code>results = []

    for level in levels:
            headers={'User-Agent': 'Mozilla/5.0'}
            res = requests.get(url+level,headers=headers)
            if res.status_code==200:
                res = json.loads(str(res.content))
                print "success"
                #do something
            else:
                print "Error",str(res.status_code)
        return results
</code></pre>

<p>In my terminal the output is</p>

<pre><code>  success
  success
  success
</code></pre>

<p>In heroku bash the output is</p>

<pre><code>  success
  Error 403
  Error 403
</code></pre>

<p>I've also tried it without the User-Agent header but same issue persists.</p>

<p>It's a permissions error. My guess is that when you run this from your browser, the first page sets various cookies, which the second and third requests require.</p>

<p>A quick possible fix, <em>IF</em> that's the issue, is to use requests' Session() objects. This stores cookies and sends them back on subsequent requests, kinda like a normal browser would.</p>

<pre><code>results = []
mySession = requests.Session()

for level in levels:
    headers={'User-Agent': 'Mozilla/5.0'}
    res = mySession.get(url+level,headers=headers)
    if res.status_code==200:
        res = json.loads(str(res.content))
        print "success"
        #do something
    else:
        print "Error",str(res.status_code)

return results
</code></pre>

python heroku