0

I have a Flask application which uses the connexion library to create the API.

The application will not just be a web service, but will also be a front end app, consuming its own API rather than making direct database calls.

How do I call, from within a Flask application, an endpoint defined as a web service within the same application?

# Create a URL route in our application for "/products"
@app.route('/products')
def products():
    """
    This function just responds to the browser ULR
    localhost:5000/
    :return:        the rendered template 'home.html'
    """
    r = requests.get('api/read/products')
    print(r.text)
    return render_template('products.html', page_title="Regulatory Analytics Home", products="")

This is simply generating:

requests.exceptions.MissingSchema: Invalid URL 'api/read/products': No schema supplied. Perhaps you meant http://api/read/products?

Changing the path to:

r = requests.get('/api/read/products')

doesn't work. My products.html file is in a /templates folder, so I tried.

r = requests.get('../api/read/products')

And this didn't work either, as I assumed I may need a relative path to the root of the site from where the products.html is served from.

smackenzie
  • 2,880
  • 7
  • 46
  • 99
  • requests expects a url and you seem to be passing a file path. Have you tried passing the url where the flask app will be hosted? – Josh Zwiebel Oct 21 '20 at 13:37
  • but thats localhost at the moment, and will change when deployed obviously – smackenzie Oct 21 '20 at 13:43
  • So until that point try passing the localhost url to the requests parameter – Josh Zwiebel Oct 21 '20 at 13:46
  • response 403 when I try this. And there is no authentication required. – smackenzie Oct 21 '20 at 14:19
  • [Try this](https://stackoverflow.com/questions/38489386/python-requests-403-forbidden) you might want to try specifying the http methods that are allowed on your endpoint. I.e methods = get – Josh Zwiebel Oct 21 '20 at 14:38
  • Thats got rid of 403, adding a user agent header, thanks. The issue now is I get a nonsense response. https://stackoverflow.com/questions/64467752/getting-incorrect-response-from-flask-using-requests?noredirect=1#comment113994623_64467752 – smackenzie Oct 21 '20 at 16:53

0 Answers0