Causes
A function handler is incorrectly defined. For example, a function handler in Python event functions contains an HTTP trigger.
Solutions
Define your function handler based on the following function types:
Python event functions. For more information, see Overview.
def handler(event, context): return 'hello world'
Python HTTP functions. For more information, see Python HTTP functions.
def handler(environ, start_response): context = environ['fc.context'] # get request_body try: request_body_size = int(environ.get('CONTENT_LENGTH', 0)) except (ValueError): request_body_size = 0 request_body = environ['wsgi.input'].read(request_body_size) print('request_body: {}'.format(request_body)) # do something here status = '200 OK' response_headers = [('Content-type', 'text/plain')] start_response(status, response_headers) # return value must be iterable return [b"Hello world!\n"]
Causes
The logic of an HTTP function is invalid. For example, the start_response
parameter is not called in the function. The following code provides an example:
def handler(environ, start_response):
# do something here
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
# forget to call start_response
# start_response(status, response_headers)
# return value must be iterable
return [b"Hello world!\n"]
Solutions
Modify the logic of your HTTP function based on the function handler and deployment framework of Python HTTP functions. For more information, see Python HTTP functions.
Causes
Returned values are set to non-iterable bytes in the Python 3 runtime environment.
Solutions
Set the returned value to iterable bytes. Example: [json.dumps(result).encode()]
.
The following example demonstrates how to modify the function code:
import json
def handler(environ, start_response):
# do something here
result = {"code": "OK"}
status = '200 OK'
response_headers = [('Content-type', 'application/json')]
start_response(status, response_headers)
# return value must be iterable
return [json.dumps(result).encode()]