Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is json.dumps() a must in Flask?

Tags:

python

flask

(This is probably a dumb question, so please wear your stupidity shields!) I've been a PHP programmer and am now learning Python + Flask. I recently had to struggle a lot with posting data through AJAX and returning a response. Finally, the code that worked was:

@app.route('/save', methods=['POST'])
def save_subscriptions():
    if request.method == 'POST':
        sites = request.form.get('selected')
        print(sites)
        sites = sites[0:-1]        
        g.cursor.execute('UPDATE users SET sites = %s WHERE email = %s', [sites, session.get('email')])
        g.db.commit()
        return json.dumps({'status': 'success'})

If I change return json.dumps({'status': 'success'}) to return 1 I get an Exception that int is not callable. First of all, I don't understand who is trying to call that int and why? Secondly, in PHP, it was frequently possible to just echo 1; and this would become the AJAX response. Why doesn't return 1 work in Flask, then?

like image 987
ankush981 Avatar asked Dec 26 '15 03:12

ankush981


People also ask

What is the difference between jsonify() and JSON dumps() in flask?

We decided to start with a common operation, the last one in the list above: returning jsonify (). jsonify () is a helper method provided by Flask to properly return JSON data. jsonify () returns a Response object with the application/json mimetype set, whereas json.dumps () simply returns a string of JSON data.

What is JSON dump in Python?

json.dump () in Python. The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json.

What is the use of JSON?

JSON is a lightweight data format which is widely used across web applications for interchanging data across and within web applications. JSON means JavaScript Object Notation. From the official docs, JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write.

What is the use of JSON module in Python?

json module in Python module provides a method called dump () which converts the Python objects into appropriate json objects. It is a slight variant of dumps () method. The dump () method is used when the Python objects have to be stored in a file.


Video Answer


1 Answers

The logic behind what should be returned by Flask views is described in the docs in detail:

The return value from a view function is automatically converted into a response object for you. If the return value is a string it’s converted into a response object with the string as response body, an 200 OK error code and a text/html mimetype. The logic that Flask applies to converting return values into response objects is as follows:

  • If a response object of the correct type is returned it’s directly returned from the view.

  • If it’s a string, a response object is created with that data and the default parameters.

  • 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.

  • If none of that works, Flask will assume the return value is a valid WSGI application and convert that into a response object.

In your case, integer 1 is returned - Flask applies the last rule and tries to convert it into a response object and fails. Internally, make_response() method is called which, in case of an integer, would call force_type() method of a werkzeug.Response class which would eventually fail creating an instance of a BaseResponse class when trying to instantiate a WSGI app:

app_rv = app(environ, start_response)

where app in your case is integer 1.

like image 96
alecxe Avatar answered Oct 10 '22 23:10

alecxe