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
Getting the same problem here.
Have tried adding a BinaryMediaTypes key to our template.yaml but no luck.
Api:
BinaryMediaTypes:
- image~1png
- image~1jpeg
- image~1jpg
- image~1pdf
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.
Same here with Python3.7 runtime, very annoying...
Is there any solution for this??
I concur. Is there any solution for this?
This one is strange, because it only fails on image types for me. Like .jpg or .png (tested this ones). If I base64 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 a multipart-blob in the event['body'] in my function.
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 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.
@joain946 Thanks! I made the change and I'm able to hit my Node app but the event body is null. Dumping it to the console, there doesn't appear to be anything relevant in the payload.
Have added BinaryMediaTypes but no joy :(
I had same issue here I am using SAM CLI 0.40.0. Is b64 encoding the only way?
I tried BinaryMediaTypes: [image/*, application/octet-stream] , but it does not work neither
- nodejs 12
Any news on this?
/ 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.
Globals:
Api:
BinaryMediaTypes: ['*~1*']
The fact that this bug has been open for ten months before this was mentioned makes this sounds like a workaround for a bug.
it still doesn't work for images, moreover, I've found an odd behaviour: if I don't specify BinaryMediaTypes it returns 502 without even invoking the function but if I do, it invokes it and returns 502 after a 30 seconds delay
it still doesn't work for images, moreover, I've found an odd behaviour: if I don't specify BinaryMediaTypes it returns 502 without even invoking the function but if I do, it invokes it and returns 502 after a 30 seconds delay
Same issue here, did you find any solution @hdgone ?
@tdhulster unfortunately no, switched to Kubernetes instead 😃
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 is
BinaryMediaTypes:
- "application/zip"
- "application/octet-stream"
Work 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 examples
Also 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 consistency
Based on my investigation I have not found any bugs here. The way it works is that you mention BinaryMediaTypes in your template file and then also pass Content-type which matches the BinaryMediaTypes e.g. In template.yaml -
BinaryMediaTypes:
- "application/zip"
- "application/octet-stream"
and then use -
curl -v -X POST 'http://localhost:3000/v1/photo-gallery' -F 'uploadedFile=@IMG_5071.JPG' -H 'Content-Type: application/octet-stream'
This will work. If you don't pass Content-type the POST data is treated as multipart/form-data. So you can put that in your BinaryMediaTypes to treat it as Binary data or use */*.
Closing this issue...