-2

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?

Everyone_Else
  • 3,206
  • 4
  • 32
  • 55

2 Answers2

3

You can issue a post using requests like so:

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

However, it's generally unnecessary to call a server from itself. You should consider abstracting out the logic within your /internal route and just calling it directly in this route.

Karin
  • 8,404
  • 25
  • 34
  • Hello Karin, when I make that change I get the same error that `TypeError: 'Request' object is not callable`. Are there any fixes? – Everyone_Else Aug 18 '16 at 04:23
  • If you replaced your call with the suggestion, there should be no `Request` anywhere in your code (you should replace the original `req = requests.Request(...)` line with the format I gave). – Karin Aug 18 '16 at 04:27
  • Sorry, good call - when I make that change the correct way, I get the error that `TypeError: 'Response' object is not callable`. What can I do about that? – Everyone_Else Aug 18 '16 at 04:29
  • 1
    I don't think this is a useful format for debugging. My suggestion would be to read up on the requests documentation (use the link I gave you), and if you get stuck again, you can ask in a separate or updated question while providing your code attempts. You should try to understand what the error `object is not callable` means though. It means you are calling something that does not have a call method. There are a lot of other Stack Overflow questions that help explain that :) – Karin Aug 18 '16 at 04:34
0

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())
Community
  • 1
  • 1
Adam
  • 3,992
  • 2
  • 19
  • 39
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/13380224) – IanS Aug 18 '16 at 12:49
  • @IanS is that better? – Adam Aug 18 '16 at 16:44