POST url encoded form to Amazon API Gateway

In the API Gateway, select the POST method for your resource, select Integration Request and create a new Mapping Template for application/x-www-form-urlencoded:

#set($body = $input.path('$'))
#set($jsonString = $util.urlencode($body))
#set($json = $util.parsejson($jsonString))

{
  "body" : $json,
}

Alternatively, you can simply pass the url encoded string:

#set($body = $input.path('$'))
{
  "body" : "$body",
}

and url decode and parse the JSON in your lambda.


The mapping template to make form data work is pretty complicated. Here is a gist: https://gist.github.com/ryanray/668022ad2432e38493df

Also, you can see this post I wrote that has an example of how to integrate with Slack(their hooks send a POST as form data to API Gateway): http://www.ryanray.me/serverless-slack-integrations


Try to set mapping template as following:

{
  "body" : $input.json('$')
}

This would convert you string into json and pass to lambda.

From amazon docs: $input.json(x) function evaluates a JSONPath expression and returns the results as a JSON string.


This is not entirely related, but if you are new to Amazon API Gateway, one additional step I did not know was required was to (re) deploy your API after adding the mapping template as others have suggested (in the case you had previously deployed your API). This cost me a bunch of debugging time as I did not understand why I was continuing to get this error even after making the suggestions posted here.

If using the AWS Console,

  • navigate to any pane within your API
  • Select Actions menu at the top
  • Select Deploy API from the menu, choose the relevant stage and confirm