question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:25 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
pyrabbitcommented, Apr 6, 2020

/ 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*']
4reactions
benjaminfiliatraultcommented, Apr 12, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting '502 BAD Gateway' while uploading image using SAM ...
When uploading a image to a SAM local Lambda function,then it fails with a 502 BAD GATEWAY. Getting 502 error before calling Lambda...
Read more >
How do I resolve HTTP 502 errors from API Gateway REST ...
For API Gateway to handle a Lambda function's response, the function must return output according to the following JSON format:
Read more >
502 BAD GATEWAY for file upload through SAM with local API ...
When attempting to upload a photo to a local Lambda running using sam local start-api --debug , it fails with a 502 BAD...
Read more >
Serverless: AWS HTTP Gateway — 502 Bad Gateway - DZone
This quick tip for serverless functions using AWS Lambda will help conquer 502 errors. Make sure you return maps instead of strings when ......
Read more >
502 Bad Gateway Error: What It Is and How to Fix It
If you're using such an application and a 502 Bad Gateway Error occurs, the issue will not be related to the app installation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found