assign variable value thought GET method in Python


I have the below python code:

from flask import Flask
from flask import request

import requests

        to_echo = request.args.get("city", "")
        response = "{}".format(to_echo)
        return response
    def check_current_weather_by_city():
        city = get_city()

What is the correct way for getting the value "Tel+aviv" into the variable city? the current implementation is not working


Broadly, requests.get() is not part of Flask (which has a subtly-different request value in a request) and is another 3rd party library which will return some Request object, which you probably want the .text property of

This could be

r = requests.get(url)
r.raise_for_status()  # make sure the request succeeded!
city = r.text

However, you are probably better off describing your logic such that the minimum amount is in the flask app and the majority is in a library you import!

Then you can directly call functions in that library if you want their logic in multiple routes without making a web request.

from mylibrary import myfunction

def foo():

def bar():

Anecdotally, this style will also make your project much easier to unit test (I have worked with several Flask projects).

Answered By – ti7

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published