0

I have a python script that performs an http request which has been working for years, but only a few months ago it stopped working.

If I do the same http request with Chrome browser, it works fine.

This is the piece of code that performs the http request:

import requests

full_url = r'http://server_name:5001/rest/.........../geocode?'
query = 'address'
country = 'gb'
user = 'user'
pswd = 'password'

res = requests.get(full_url, params={'query': query, 'country': country}, auth=(user,pswd))

If I check the value of 'res' I can see it's 403, which I believe means access has not been granted. However, I know user and pswd are correct because, as stated above, if I do the request via Chrome browser, it works fine.

Any ideas what the problem could be?

Pitrako Junior
  • 143
  • 1
  • 7
  • 1
    Did they switch to https (SSL) ? – Mike67 Jul 31 '20 at 18:46
  • I have tried with https as well, with the same result (and http works in the browser). – Pitrako Junior Jul 31 '20 at 18:50
  • As mentioned, you may have been redirected. Check the lock icon next to the url. Better yet, copy/paste the url from chrome after success and see what it is. – tdelaney Jul 31 '20 at 18:51
  • I actually did that already. I copied the url from chrome after success and used it in my script (no success). The url looks the same except spaces have been changed for %20. – Pitrako Junior Jul 31 '20 at 18:56
  • Looks like you are making call to HTTP. If they have moved to HTTPS, try passing the argument like verify=False in you request.get() method. Also add more information to your question by what error you receive in the traceback calls. – Raviteja Ainampudi Jul 31 '20 at 19:04
  • If they had moved to https, then http would not work in chrome browser, but it does. Anyway, I have tried adding verify=False, but it didn't work. I still get 403. – Pitrako Junior Jul 31 '20 at 19:08
  • 403 is a Forbidden error. Usually Some API's restrict their responses to few sdk s. Though your credentials are working on GUI. Make sure they are available or permissions are correct while accessing through the REST API. – Raviteja Ainampudi Jul 31 '20 at 19:18
  • @Raviteja: How can I make sure credentials are available or permissions are correct while accessing the REST API? What do I need to do? Thx. – Pitrako Junior Jul 31 '20 at 19:21
  • Sometimes APIs try to limit access to only browsers. You can try to fake a browser call by adding a user agent https://stackoverflow.com/questions/27652543/how-to-use-python-requests-to-fake-a-browser-visit – chrislondon Jul 31 '20 at 19:33
  • I have tried what chrislondon suggested, to no avail. – Pitrako Junior Jul 31 '20 at 20:08
  • @PitrakoJunior It is usually the API providers who can fix the permissions for credentials to that REST API. The best is to either reach their support team or dig in their support documents about their API. – Raviteja Ainampudi Jul 31 '20 at 20:43

0 Answers0