2

I'm sending data via AngularJS POST request:

$http.post('/process', { 'uid': uid, 'action': action }).success(function(response) {
    console.log(response);
});

And trying to get sended values in Flask

@app.route('/process', methods = ['POST'])
def process():
    return json.dumps({ 'data': request.form.get('uid', 'EMPTY') })

And Flask returns back {"data": "EMPTY"} response. request.form is empty. I've tried to get data from request.data, but it's in strange format there.

I'm learning Python and Flask so I want to do this work with native library, without any other packages right now.

mikatakana
  • 503
  • 9
  • 22

3 Answers3

5

get_json() method helps me

@app.route('/process', methods = ['POST'])
def process():
    return json.dumps({ 'data': request.get_json().get('uid') })
mikatakana
  • 503
  • 9
  • 22
0

If you want to get the data via request.form, you need to have angular send it differently. See this question for details: How can I post data as form data instead of a request payload?

Community
  • 1
  • 1
Zags
  • 37,389
  • 14
  • 105
  • 140
0

Make sure Content-Type is application/json

Here's a tutorial series on using Flask and AngularJS together - http://tutsbucket.com/tutorials/building-a-blog-using-flask-and-angularjs-part-1/

Hope you'll find it useful.

John Kenn
  • 1,607
  • 2
  • 18
  • 31