0

I am trying to create a 'pipe' between an angular frontend and a python flask backend. I have managed to communicate from the former to the latter by HttpClient.get calls but updating that to HttpClient.post breaks the communication. My code looks like:

  • on the Angular side:

    let request =
    this.HttpClient.post(`http://127.0.0.1:5000/weather/loc`,
    {
      "location": this.location,
    })
    
    request.subscribe((data) => {
      console.log(data); })
    

and on the flask side:

@app.route('/weather/loc', methods=["POST"])
def weather_connection():
    print( request.form)
    location = request.form.get("location", default="London")
    #more code

The problem I see is that request.form is always ImmutableMultiDict([]) an empty dictionary. For some reason the location argument seems to have been lost somewhere.

1 Answer 1

1

Angular's HttpClient posts data in JSON format. But Flask's request.data is only for data that is form-encoded. Instead, you should use request.get_json().

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, using location = request.json["location"] does the trick. Many thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.