14

Building an app using flask. The app uses a table structure to display data. Part of its functionality is collecting the data from user specified table rows. To do this I have placed a button on each row that executes some js. The js collects the information from the row, uses JSON.stringify() to convert to json object and the issues the post request to the relevant flask url.

Logging the value of the jsonified object to the browser console from js file shows it is correctly formed. The post request contacts the correct route however the request.get_json() function returns a value of None in the method of that route.

I have set up a seperate route in flask for testing. Here is the relevant code

from javascript

    function do_some_work(e) {
        var row_data = get_table_row_data(e);
        row_data = JSON.stringify(row_data);
        console.log(row_data);
        $.post("test", row_data);
    }

get_table_row_data() simply returns an object with key:value pairs. The log shows the data is correctly formatted json.

And here is the python code

#TODO
@application.route('/test', methods=['GET', 'POST'])
def test():
    data = request.get_json()
    print("data is "+format(data))
    return redirect(url_for('index'))

Here data is coming up as None

any help much appreciated

Philip Butler
  • 479
  • 1
  • 5
  • 13
  • 2
    If I understand correctly, the referred post 'How to get POSTed json in Flask?' was asking users to use get_json(), which is what the author was doing and turned out not working. It didn't seem to be a duplicated question to me. – Suicide Bunny Jul 23 '18 at 21:55
  • The canonical has 775k+ views and 15+ answers that cover virtually every aspect of POSTing JSON in Flask, with and without jQuery. [The author themselves agreed it was a dupe](https://stackoverflow.com/questions/49010415/flask-request-get-json-returning-none-when-valid-json-data-sent-via-post-reque#comment85027055_49010614). For future visitors--please just [check out the canonical](https://stackoverflow.com/questions/20001229/how-to-get-posted-json-in-flask/)! – ggorlen Jul 24 '22 at 16:25

1 Answers1

6

It's request.json it will return a dictionary of the JSON data. To get a value you use request.json.get('value_name'). So your route will be like this

#TODO
@application.route('/test', methods=['GET', 'POST'])
def test():
    data = request.json
    print("data is " + format(data))
    return redirect(url_for('index'))
Marvin
  • 465
  • 2
  • 6
  • 14
  • solved. Its always going to be something stupid like that. Thanks very much – Philip Butler Feb 27 '18 at 14:11
  • no problem, glad it solved your problem – Marvin Feb 27 '18 at 14:15
  • ugh, my bad. Problem is still there and the same – Philip Butler Feb 27 '18 at 14:17
  • still returning None data = request.json print('Data is '+format(data)) – Philip Butler Feb 27 '18 at 14:17
  • first add an if statement to check if it's a post request `if request.method == 'POST'` then try getting a single value from the json data with `request.json.get('data_in_json')` – Marvin Feb 27 '18 at 14:26
  • yes it is a post request. trying to get a single value from the json data throws the following error "AttributeError: 'NoneType' object has no attribute 'get'" – Philip Butler Feb 27 '18 at 14:31
  • also in your request, you should set `Content-Type` header to `application/json` else it'll return `None`. And sorry, you can use the `request.get_json(force=False)` to get it even if the header is not set – Marvin Feb 27 '18 at 14:33
  • 16
    data = request.get_json(force=True) <--solved the issue. Thanks again folks. And whoever downvoted is correct. This is a duplicate question. Apologies – Philip Butler Feb 27 '18 at 14:40
  • 1
    @PhilipButler to elaborate a little: 'force – if set to True the mimetype is ignored', so the call you're making either does not include a MIME type or the wrong one. You can either set the correct MIME type in your request ('application/json') or set 'force' to 'True', but then you need to be sure it is indeed JSON you're receiving. – kasimir Nov 09 '20 at 13:14
  • Thanks, Philip. I was also facing the same issue. Adding force = True solved my issue :) – Sarath Subramanian Jul 10 '21 at 14:17