aws-sam-cli: 502 BAD GATEWAY for file upload through SAM with local API Gateway
Description
When attempting to upload a photo to a local Lambda running using sam local start-api --debug
, it fails with a 502 BAD GATEWAY. No code within the Lambda is executed.
Steps to reproduce
I used curl to do the upload.
curl -v -X POST 'http://localhost:3000/v1/photo-gallery' -F 'uploadedFile=@IMG_5071.JPG
This Lambda uses Flask to handle multiple endpoints and the others are working as expected.
Observed result
Please provide command output with --debug
flag set.
SAM does not display any debug output. It just logs the request with the response set as a 502.
Expected result
I would expect that the lambda would be executed.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: Mac
sam --version
: SAM CLI, version 0.17.0
Add --debug flag to command you are running
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 7
- Comments: 25 (2 by maintainers)
/
need to be written as~1
as per the documentation. After playing around I finally got the following to work. Keep in mind that this allows all binary media types.same issue here when uploading a
.txt
file it’s working fine but I’m trying a.zip
file which throw an 502 BAD GATEWAY error. API Gateway binary type set isWork around
As mentioned by pyrabbit, set the BinaryMediaTypes to
['*~1*']
In the documentation AWS says "Use
~1
instead of/
in the mime types. " but"application~1zip"
doesn’t work, maybe the documentation should add more examples of how to use the BinaryMediaTypes with working examplesAlso on the doc AWS says “This property is similar to the BinaryMediaTypes property of an AWS::ApiGateway::RestApi resource.” which uses the
/
maybe a change on the SAM CLI to support the/
should be good as we keep the same behaviors and consistencyI had the same issue with SAM CLI 0.19.0. I managed to resolve it by adding “application/octet-stream” to “BinaryMediaTypes” in the SAM yaml file. So now my entry looks like this:
Properties: BinaryMediaTypes: [image/*, application/octet-stream]
With my Java Lambda I can now get the binary data in a “byte[]” input object. I haven’t verified this in a deployed version yet.Solved it by having a template like this one
This one is strange, because it only fails on image types for me. Like
.jpg
or.png
(tested this ones). If Ibase64
encode my data prior to sending (e.g.base64 image.jpg > image64; curl ... -F 'image64=@image64'
) works perfectly and I can retrieve the data as amultipart
-blob in theevent['body']
in my function.@prenx4x actually, there is still a problem if one uses HttpApi events for lambdas.
I tried to set
as well as
But no luck, still getting 502 (BAD GATEWAY) while trying to send files to this lambda.
I need especially
HttpApi
type of event insteand ofApi
because the platform for which I am developing requires lambdas to processAPIGatewayProxyEventV2
. Everything been set up so far, but now I am struggling to create a lambda with the file upload and test it locally.BTW, I am trying to test file sending by putting this code into my Chrome Dev Tools:
(changing headers didn’t help too when there was single file; also my app is required to be able to upload multiple files at once).
Maybe I should create separate issue for this? Thanks.
Have added BinaryMediaTypes but no joy 😦
I have the same issue, though I am sending a small
.wav
file, not an image.Local output with
--debug
gives no extra information, it just returns a 502 before attempting to execute anything.With the same configuration actually deployed to AWS, I can send a file without issue. The issue only occurs with the local api.
I’m having the same issue. I also see the same thing as @yehorb that if i create a txt file contents of the base64 encoding of an image then it works just fine.
I concur. Is there any solution for this?
Is there any solution for this??
Same here with Python3.7 runtime, very annoying…
Getting the same problem here.
Have tried adding a
BinaryMediaTypes
key to ourtemplate.yaml
but no luck.