-1

This is my Python code:

@app.route("/getdata", methods=['GET'])
def get_User():
myuser= User 
myuser=User.query.all()
    if myuser =="":
        return 404
    return HttpResponse(myuser, content_type="application/json")

I am trying to make sure the data I send is in JSON format. but it is giving me an error. Can I not use HttpResponse in Flask?

Prasanna
  • 79
  • 2
  • 11

1 Answers1

2

Use flask.Response:

http://flask.pocoo.org/docs/0.12/api/#flask.Response

return flask.Response(myuser, content_type="application/json")

If myuser is not actually JSON, which appears to be the case for you, you can:

import json 
myuser = json.dumps([u.as_dict() for u in User.query.all()])

You might also consider jsonify which is built into flask: https://stackoverflow.com/a/13172658/4225229

JacobIRR
  • 8,545
  • 8
  • 39
  • 68