user17833241
user17833241

Reputation:

How to make a GET request to Flask server

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!

Upvotes: 0

Views: 546

Answers (2)

Babatunde Mustapha
Babatunde Mustapha

Reputation: 2663

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

print(x.content)

Upvotes: 0

Pratham180
Pratham180

Reputation: 25

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.

Upvotes: 0

Related Questions