Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Python Flask to return application/json using json.dumps

I am using Python Flask to create a server. I want to send application/json back to the client (my customer requires this). When I use json.dumps() it is sending back text.

(Incase your wondering, if I use jsonify() instead of json.dumps(), I do get application/json, however this function seems to have problems with some data (if I dont get a solution here, I will look further into jsonify)).

server..............

import json
from flask import Flask, render_template, url_for, current_app, request, jsonify

app = Flask(__name__)

@app.route("/<arg1>")
def route1(arg1):
    dict1 = {"prop1": "p1", "prop2": "p2"}
    #return jsonify(dict1)      # this sends 'application/json'
    return json.dumps(dict1)  # this sends ' text/html; charset=utf-8'

app.run(host="0.0.0.0", port=8080, debug=True)

Test client (actual client is provided by customer) ......

import pycurl
import cStringIO

SERVER_URL = "http://192.168.47.133:8080"  

buf = cStringIO.StringIO()
c = pycurl.Curl()

c.setopt(c.URL, SERVER_URL + '/index.html?city=perth')
c.setopt(c.HTTPHEADER, ['Accept:application/json']) 
c.setopt(c.WRITEFUNCTION, buf.write)
c.setopt(c.VERBOSE, True)

c.perform()
buf1 = buf.getvalue()
buf.close()

print(buf1)
like image 378
spiderplant0 Avatar asked Sep 04 '14 13:09

spiderplant0


People also ask

How do I get JSON from request Flask?

You need to set the request content type to application/json for the . json property and . get_json() method (with no arguments) to work as either will produce None otherwise.

What is the difference between Jsonify and JSON dumps?

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. This could lead to unintended results.

How to return JSON from a GET request in flask?

@app.route ('/path_of_the_response', methods = ['GET']) def ReturnJSON (): pass Inside the ‘ReturnJSON’ function if the request method is ‘GET’ then create a python dictionary with the two elements message. Jsonify the python dictionary and return it. Build the flask application using the following command.

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.

How to return JSON from a GET request in Python?

Route the ‘ReturnJSON’ function to your desired URL using the following syntax. @app.route ('/path_of_the_response', methods = ['GET']) def ReturnJSON (): pass Inside the ‘ReturnJSON’ function if the request method is ‘GET’ then create a python dictionary with the two elements message. Jsonify the python dictionary and return it.

How to run an application in flask with Python?

This tutorial assumes the user to have the basic knowledge of Python programming language and Flask framework. Once you have Flask and Python installed in your system, create a file called app.py and paste the following code: Try running the app.py file and you should be able to view the application running at http://localhost:5000/getData.


1 Answers

Change it like this:

from flask import Response

@app.route("/<arg1>")
def route1(arg1):
    dict1 = {"prop1": "p1", "prop2": "p2"}
    return Response(json.dumps(dict1), mimetype='application/json')
like image 193
mehdix Avatar answered Nov 04 '22 23:11

mehdix