Everyone_Else Everyone_Else - 3 months ago 11
Python Question

How to combine flask and requests in python?

On the page

internallogin
, if the user authenticates, I'd like to make a post request. To that effect, I have the following code.

@app.route('/internallogin', methods=['POST', "GET"])
def showInternallogin():
uname = request.form.get("name")
passw = request.form.get("pass")
if is_valid_login(uname, passw):
print("legal_login")
req = requests.Request(method="POST", url='http://localhost:5000/internal', data={"name": uname, "passw": passw})
return req
else:
return redirect('/login')


What happtens is immidiately after printing "legal_login" is that I get the error that
TypeError: 'Request' object is not callable
. How can I make a post request using flask?

Answer

The following is right from @Karin answer

response = requests.post('http://localhost:5000/internal', data={...})

Here might be the reason why you are receiving that error.

Return a requests.Response object from Flask

If a tuple is returned the items in the tuple can provide extra information. Such tuples have to be in the form (response, status, headers) where at least one item has to be in the tuple. The status value will override the status code and headers can be a list or dictionary of additional header values.

This should likely solve it.

return (req.text, req.status_code, req.headers.items())
Comments