TypeError: <Response 36 bytes [200 OK]> is not JSON serializable

15.1k views Asked by At

I am writing web service using restful flask. The below code giving me this error - TypeError: is not JSON serializable

from flask import jsonify
from flask_restful import Resource
class Recipe(Resource):
   def get(self):
      return jsonify({"status": "ok", "data": ""}), 200

How ever this code is working fine

from flask import jsonify
from flask_restful import Resource
class Recipe(Resource):
   def get(self):
      return jsonify({"status": "ok", "data": ""})

The Below code is also working

from flask import jsonify
from flask_restful import Resource
class Recipe(Resource):
def get(self):
   return {"status": "ok", "data": ""},200

I have noticed that I get the error when I use jsonify and response code together, I need to use jsonfy because I will be sending object as response.

2

There are 2 answers

2
Codeformer On BEST ANSWER

Got the solution - Flask has this function called make_response

from flask import jsonify, make_response

class Recipe(Resource):
   def get(self):
   return make_response(jsonify({"status": "ok", "data": ""}), 201)
0
Mahmoud Magdy On

I have the same problem but I realized that I made a small mistake when I was trying to convert the result of jsonify method to str.

str(jsonify({'code':200, 'message': 'test'}))