MartinSahlen/cloud-functions-python

Flask response TypeError: is not JSON serializable

Closed this issue · 5 comments

Running using cloud-function-python using flask

Can get the data but getting 500 error like this

How do I fix this issue?

[13] Failed to execute script function Traceback (most recent call last):
File "function.py", line 34, in File "site-packages/cloudfn/flask_handler.py", line 50, in handle_http_event File "json/init.py", line 230, in dumps File "json/encoder.py", line 198, in encode File "json/encoder.py", line 256, in iterencode File "json/encoder.py", line 179, in default TypeError: b'{\n "json": {\n "empID": "I123", \n"refID": "69", \n "refTable": "123456"\n }, \n "message": "Hello world!"\n}\n' is not JSON serializable

Anyone help me out here?

I even tried changing json.dumps to json.dump on line 50 inside flask_handler.py

Then built the pycloudfn using python setup.py build install

But still then I'm getting the same error. I'm confused and fed up!

Is there anyone here who can help me out with this?

Was told issue is with cloud-function-python not flask

Maybe stupid question from me but it seems you are passing a binary string as json. Might that be the issue?

Here's my function.py

from cloudfn.flask_handler import handle_http_event
from cloudfn.google_account import get_credentials
from flask import Flask, request
from flask.json import jsonify
from google.cloud import bigquery
from flask_cors import CORS
from FTUL import mainP
import json

app = Flask('the-function')
biquery_client = bigquery.Client(credentials=get_credentials())
cors = CORS(app, resources={r"/*": {"origins": "*"}})

#@app.route('/',  methods=['OPTIONS'])
#def handle_request():
#    return jsonify(message='Success'), 201

@app.route('/',  methods=['OPTIONS','POST'])
def handle_request():
	#print("######## Request Header: ############# ",request.headers)
	#print("######## Request JSON: ############# ",request.get_json())
	#rBody = request.get_json()
	#print("refID",rBody['refID'])
	#im = rBody['image']
	#rID = rBody['refID']
	#rTable = rBody['refTable']
	#uAlgo = 0
	#empID = rBody['empID']
	#val = mainP(im,rID,rTable,uAlgo,empID)
	#response_status = {"message":"Success"}
	return jsonify(message='Hello world!', json=request.get_json()), 201

handle_http_event(app)

This is how I test my cloud function

screen shot 2018-04-13 at 6 32 11 pm