Home > database >  Flask jsonify returns Response. How to provide status code?
Flask jsonify returns Response. How to provide status code?

Time:04-11

I sow a lot of examples where developers have the following expression:

return jsonify(data), 200

Even in my last work project I had the same expression, but now, when I'm trying to write my new project, I catch the following error:

TypeError: Object of type Response is not JSON serializable

If I return only the result of jsonify(), it works but returns the 200 status code. I want to control the code. It doesn't work with flask_restful.Resource.

Seems like jsonify() returns Response object. How to fix it?

CodePudding user response:

Sounds like data is a Response object, and the code is choking when you try to serialize that into JSON. Try return jsonify(data.text), 200.

If that doesn't work, we need more code to diagnose this.

CodePudding user response:

flask-restful uses jsonify already.

if you use jsonify with flask-restful, it tries to jsonify a response twice.

simply do this:

return data, 200
  • Related