8

I am trying to get the form data from a multipart/form-data POST to my ASW Lambda web service via API Gateway.

The HTTP POST has Content-Type "multipart/form-data" and body that is URL encoded. File data is also sent in this post (hence the multipart, I guess).

The web service needs to integrate with a thirdparty service, so changing the format of the POST isn't really an option.

I have seen this thread talking about converting the URL encoded data to JSON object for use in Lambda, but this doesn't do the trick.

I have also tried setting the Integration Request -> Mapping Templates for content type multipart/form-data to Input passthrough. This didn't help either.

I did come across another question about uploading a file using multipart/form-data, but since I'm not interested in the file, just the body, that answer didn't help.

Below find screenshot (sorry) of the captured post via runscope.

Post details

Community
  • 1
  • 1
Diederik
  • 5,536
  • 3
  • 44
  • 60

1 Answers1

5

If the goal is to use Lambda, you'll need to pass valid JSON to the function. Currently there isn't a way to JSON-ify data inside Api Gateway that comes in as non-JSON data.

Our short term fix (on our backlog) is to provide a variable in the mapping templates to grab the raw input of the request. That way you could do a simple JSON conversion using a template like:

{
    "body" : "$input.body"
}

or something like that.

Check out the mapping template reference for more info: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

Edit 4/7 - feature has been released as $input.body

jackko
  • 6,998
  • 26
  • 38
  • Thanks for the answer. I suspected that it was a JSON only service. (Not too unreasonable given the direction most Web APIs are heading.) I worked around this restriction by putting another service in between, that does the translation. – Diederik Dec 18 '15 at 06:29
  • Hi @Jack is this `$input.raw` variable available yet? – Tiago Lopo Dec 29 '15 at 03:04
  • Not yet, we're still working on getting through a lot of other items on the backlog. No ETA at this point, unfortunately. My apologies! – jackko Feb 04 '16 at 02:53
  • Hey @JackKohn-AWS, I think without this feature there's no way to use signatures to verify incoming requests from webhooks like Github - https://developer.github.com/webhooks/securing/ . This is a pretty limiting feature for us. Any updated ETA on this? – jjj Mar 28 '16 at 19:52
  • 1
    To answer my own question above, AWS has added a mapping called `"$input.body"`, see - https://forums.aws.amazon.com/thread.jspa?threadID=228067 – jjj Apr 07 '16 at 19:08
  • @Diederik can you tell exactly which service did you use to do the translation? – girish Dec 21 '20 at 04:41
  • The solution is to use a mapping template to get the whole (non-JSON as I understand it - disclaimer, I didn't use this workaround for now) body passed to Lambda. (The service the Lambda interacted with was a third-party service, so I couldn't change what it was sending the function.) – Diederik Dec 22 '20 at 19:11