Error KeyError 'headers' in serverless_wsgi

Serverless newbie here. I’m stuck getting the following error thrown when I do a test “POST” call to a Flask route directly in the Lambda (AWS UI):

“errorMessage”: “‘headers’”,
“errorType”: “KeyError”,
“stackTrace”: [
" File “/var/task/serverless_sdk/”, line 134, in wrapped_handler\n return user_handler(event, context)\n",
" File “/var/task/”, line 110, in handler\n return serverless_wsgi.handle_request(wsgi_app, event, context)\n",
" File “/var/task/”, line 94, in handle_request\n headers = Headers(event[u\“headers\”])\n"

The root Flask route “/” responds as expected, returning a simple message.

Everything works perfectly when I run locally using ‘serverless wsgi serve’.

I’m considering that Flask requires certain headers ( - which it is not being passed when I do this test, but when I try to test with through the API Gateway or externally from Postman I get a “502 Bad Gateway” and “message”: “Internal server error” response.

Same error here, even using sls invoke local

  "body" : $input.json('
  "headers": {
    #foreach($header in $input.params().header.keySet())
    "$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end

  "method": "$context.httpMethod",
  "params": {
    #foreach($param in $input.params().path.keySet())
    "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end

  "query": {
    #foreach($queryParam in $input.params().querystring.keySet())
    "$queryParam": "$util.escapeJavaScript($input.params().querystring.get($queryParam))" #if($foreach.hasNext),#end


Not sure if you’re still having trouble with this but this worked for me:

sls invoke local -f <function> --data '{"headers": {}, "path":"/", "body": {}, "httpMethod": "GET"}'

It’s just manually passing in the event data for the function so edit the data for the function method you want to call.

You can also make a json for the event data and just pass in the path to the file

--path path/to/data.json