Home > Mobile >  How to make a GET request to Flask server
How to make a GET request to Flask server

Time:01-06

So, I've made a Flask server using Python on REPL and I'm trying to tell people how to use it. To do so, I'm making an example using Python. So far I have:


import requests
baseURL = "https://myurl.repl.co"
x = requests.get(baseURL)
print(x)
But what I get is: <Response [200]>
When I try to get an item from there, I get: Traceback (most recent call last): File "main.py", line 6, in <module> print(x[0]) TypeError: 'Response' object is not subscriptable

Does anyone know how to properly make a GET request to the server? Thanks!

CodePudding user response:

import requests
baseURL = "https://myurl.repl.co"
x = requests.get(baseURL)

print(x.content)

CodePudding user response:

Install the requests module (much nicer than using urllib2) and then define a route which makes the necessary request - something like:

import requests
from flask import Flask
app = Flask(__name__)

@app.route('/some-url')
def get_data():
    return requests.get('http://example.com').content

Depending on your set up though, it'd be better to configure your webserver to reverse proxy to the target site under a certain URL.

  •  Tags:  
  • Related