tj89
tj89

Reputation: 4151

Making request to API using python

I have created and API using python+flask. When is try to hit the api using postman or chrome it works fine and I am able to get to the api.

On the other hand when I try to use python

import requests
requests.get("http://localhost:5050/")

I get 407. I guess that the proxy of the our environment is not allowing me to hit the localhost. But due to LAN settings in IE/Chrome the request went through.

I did try to set proxies , auth in request and now I start getting 502(bad gateway). If I see on the API side I can't see a request come through. What can I do to troubleshoot the same.

Upvotes: 0

Views: 247

Answers (2)

kartikey
kartikey

Reputation: 370

Try

import requests
from flask_cors import CORS, cross_origin

app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
requests.get("http://localhost:5050/")

Upvotes: 0

eugenci
eugenci

Reputation: 183

According to requests module documentation you can either provide proxy details through environment variable HTTP_PROXY (in case use Linux distribution):

$ export HTTP_PROXY="http://corporate-proxy:port"
$ python
>>> import requests
>>> requests.get('http://localhost:5050/')

Or provide proxies keyword argument to get method directly:

import requests

proxies = {
  'http': 'http://coporate-proxy:port',
}

requests.get('http://localhost:5050/', proxies=proxies)

Upvotes: 1

Related Questions