3

I have api when called using Postman returns 200 and expected data. Similarly when urllib is used it returns 200 but fails when used requests.

Note - All methods work correctly on external url but return 401 on localhost

urllib code(returns 200):

r = request.Request(myUrl)
r.add_header('Accept', 'application/json')
r.add_header('Authorization', f'Bearer {self.AUTH_TOKEN}')

response = request.urlopen(r, context=self.ctx)

requests code (returns 401):

headers = {
            'Authorization': f'Bearer {self.AUTH_TOKEN}',
            'Accept': 'application/json'
            }
response = requests.get(url = myUrl,
                        headers = headers,
                        verify=False)

When checked, it looks like bearer token is removed from headers.

response.request.headers :

{'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'application/json', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache'}

response.headers :

{'Date': 'Sun, 15 Mar 2020 04:26:40 GMT', 'Server': 'Kestrel', 'Content-Length': '0', 'WWW-Authenticate': 'Bearer', 'X-Frame-Options': 'SAMEORIGIN'}
Sria Pathre
  • 182
  • 1
  • 10

0 Answers0