Aws-sam-cli: 502 BAD GATEWAY for file upload through SAM with local API Gateway

Created on 7 Jun 2019  路  18Comments  路  Source: aws/aws-sam-cli

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

arelocastart-api priorit2-important typbug

Most helpful comment

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

All 18 comments

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 馃槂

Was this page helpful?
0 / 5 - 0 ratings

Related issues

burck1 picture burck1  路  45Comments

uvaisTrivecta picture uvaisTrivecta  路  40Comments

kaukas picture kaukas  路  24Comments

0xdevalias picture 0xdevalias  路  27Comments

GeoffreyBooth picture GeoffreyBooth  路  29Comments