aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

502 BAD GATEWAY for file upload through SAM with local API Gateway

Open smpeters opened this issue 7 years ago • 26 comments

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)

  1. OS: Mac
  2. sam --version: SAM CLI, version 0.17.0

Add --debug flag to command you are running

smpeters avatar Jun 07 '19 17:06 smpeters

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

RuNpiXelruN avatar Jul 12 '19 06:07 RuNpiXelruN

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.

Checkroth avatar Jul 16 '19 06:07 Checkroth

Same here with Python3.7 runtime, very annoying...

cmorgia avatar Aug 01 '19 16:08 cmorgia

Is there any solution for this??

arnelcabinti avatar Sep 05 '19 13:09 arnelcabinti

I concur. Is there any solution for this?

TiS avatar Sep 30 '19 13:09 TiS

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.

yehorb avatar Oct 01 '19 18:10 yehorb

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.

geofflancaster avatar Oct 03 '19 02:10 geofflancaster

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 avatar Oct 03 '19 06:10 joain946

@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.

geofflancaster avatar Oct 04 '19 05:10 geofflancaster

Have added BinaryMediaTypes but no joy :(

canvaspixels avatar Jan 30 '20 17:01 canvaspixels

I had same issue here I am using SAM CLI 0.40.0. Is b64 encoding the only way?

ruodingt avatar Feb 05 '20 22:02 ruodingt

I tried BinaryMediaTypes: [image/*, application/octet-stream] , but it does not work neither

  • nodejs 12

zhaozhongming avatar Feb 21 '20 02:02 zhaozhongming

Any news on this?

k3vur avatar Mar 24 '20 15:03 k3vur

/ 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*']

pyrabbit avatar Apr 06 '20 00:04 pyrabbit

The fact that this bug has been open for ten months before this was mentioned makes this sounds like a workaround for a bug.

smpeters avatar Apr 06 '20 12:04 smpeters

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

hdgone avatar Apr 26 '20 13:04 hdgone

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 avatar Jun 23 '20 10:06 tdhulster

@tdhulster unfortunately no, switched to Kubernetes instead 😃

hdgone avatar Jun 23 '20 15:06 hdgone

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

benjaminfiliatrault avatar Apr 11 '21 17:04 benjaminfiliatrault

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...

prenx4x avatar Jul 14 '21 20:07 prenx4x