I have a basic Python Flask API set up right not that takes an ID as a parameter for a GET request. Inside the get request logic, I am doing a get request on another resource to retrieve an image associated with the ID that was passed in:
image = requests.get(url = image_url)
image = Image.open(BytesIO(image.content))
print(image.format)
The image format that it returns in JPEG
.
I want to add this as part of the response of my API. This is the definition of my GET endpoint:
@app.route('/getmsg/', methods=['GET'])
def respond():
This is how I am trying to return the image that I got from the other resource:
return {"image": send_file(image, mimetype='image/jpeg'), "data": jsonify(return_list)}
I already tried following this stackoverflow question: How to return images in flask response?
Which is how I got to where I am at now. I make a get request through this endpoint: http://localhost:5000/getmsg/?id=7187167507933759112
.
I have also tried to return just the image like so:
return send_file(image, mimetype='image/jpeg')
But it gives me the same error.
Everything works except the returning of the image.
I expect to be able to see the image in the response of the get request, but right now it gives 500 internal server error.
This is the response that I get in the terminal:
TypeError: <Response 693 bytes [200 OK]> is not JSON serializable
Any guidance on what I am doing wrong would be greatly appreciated. Thanks.