Get non file body from multipart/form-data using AWS API Gateway and Lambda

若如初见. 提交于 2019-12-09 11:36:05

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/33981482/get-non-file-body-from-multipart-form-data-using-aws-api-gateway-and-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!