0

For some reason, I am not getting a json as my response. What am I doing wrong? I want to be able to get the result dict when I call the post function but all I am getting back is a 200 status code indicating everything is ok.

@app.route('/data', methods=['POST'])
def data():
    if request.method == "POST":  
        result = {
                    "data": {
                        "name": "First, Last",
                        "value": 1
                      }
                    }
        return Response(json.dumps(result), mimetype='application/json; charset=utf-8')
    else:
        abort(404)

I have tried different ways to return the json obect:

return json.dumps(result)

return jsonify(result)

But nothing works :\

deeformvp
  • 157
  • 1
  • 2
  • 11
  • Why do you think that response doesn't contain json? Your code return json. How do you make request? – Greg Eremeev Mar 04 '17 at 06:59
  • I ran your code and made request via rest client. It's ok. – Greg Eremeev Mar 04 '17 at 07:05
  • @Budulianin I basically am running the application on my localhost and then using ngrok to get a working url. I am calling post with the postman chrome app but I am not getting back a json at all. How have you done it? – deeformvp Mar 04 '17 at 07:21
  • Flask==0.12, Python 2.7.13, my code https://codeshare.io/ampnrp I run it thru python -m flask run, I make request thru Restlet Client - DHC. – Greg Eremeev Mar 04 '17 at 09:07

1 Answers1

1

I am not sure but could it be, that the url /data is used somewhere else and you never call this specific function. Can you add some logging or prints in the function to verify that the function is called. I tried your your code with python 2.7 and Flask 0.12 in a simple example and it works.

 # -*- coding: utf-8 -*-

import json
from flask import Flask, jsonify, Response, request

# Initialize the Flask application
app = Flask(__name__)

# Set some configuration to the flask ap
app.config['DEBUG'] = True


# Default route for /
@app.route("/data", methods=['POST'])
def index():
    if request.method == "POST":
        result = {
            "data": {
                "name": "First, Last",
                "value": 1
            }
        }
        # Both worked 
        # return jsonify(result)
        return Response(json.dumps(result), mimetype='application/json; charset=utf-8')
    else:
        return abort(404)

Exported the file as FLASK_APP export FLASK_APP=server.py and run the server flask run

N. Mauchle
  • 494
  • 1
  • 7
  • 20