aws-sam-cli: 502 BAD GATEWAY when trying to upload files to lambda which is set up via SAM local API Gateway with HttpApi events

There was no response in #1216 and there was a warning recommending to create a new issue. Actually this one seems to be pretty old, but for some reason it was closed a week ago. Unfortunately, provided solutions do not work in described case.

Description:

I tried to set

Globals:
  Api:
    BinaryMediaTypes:
      - '*~1*'
      - '*/*'
      - 'multipart~1form-data'
      - 'multipart/form-data'
      - 'application~1octet-stream'
      - 'application/octet-stream'

as well as

Resources:
  CrudDebug:
    Type: AWS::Serverless::Function
    Properties:
      Handler: dist/api/functions/crud/index.debug
      Runtime: nodejs14.x
      Timeout: 10
      Environment: *ENV
      Events:
        HttpGet:
          Type: HttpApi
          Properties:
            Path: /crud-debug
            Method: any
            BinaryMediaTypes:
              - '*~1*'
              - '*/*'
              - 'multipart~1form-data'
              - 'multipart/form-data'
              - 'application~1octet-stream'
              - 'application/octet-stream'

But no luck, still getting 502 (BAD GATEWAY) while trying to send files to this lambda.

I need especially HttpApi type of event insteand of Api because the platform for which I am developing requires lambdas to process APIGatewayProxyEventV2. 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:

let formData = new FormData();
formData.append('file', (await (await fetch(`https://placekitten.com/300/225?_=${+new Date()}`)).blob()));
formData.append('file', (await (await fetch(`https://placekitten.com/400/300?_=${+new Date()}`)).blob()));
await (await fetch('http://127.0.0.1:3000/crud-debug', {
  method: 'post',
  body: formData,
//   processData: false,
//   contentType: false,
  headers: {
//     'Content-Type': 'application/octet-stream',
  },
})).json();

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

Steps to reproduce:

let formData = new FormData();
formData.append('file', (await (await fetch(`https://placekitten.com/300/225?_=${+new Date()}`)).blob()));
formData.append('file', (await (await fetch(`https://placekitten.com/400/300?_=${+new Date()}`)).blob()));
await (await fetch('http://127.0.0.1:3000/crud-debug', {
  method: 'post',
  body: formData,
//   processData: false,
//   contentType: false,
  headers: {
//     'Content-Type': 'application/octet-stream',
  },
})).json();

Observed result:

502 BAD GATEWAY

Expected result:

200 OK

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Microsoft Windows [Version 10.0.19042.1110]
  2. sam --version: 1.26.0
  3. AWS region: local

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 4
  • Comments: 17 (2 by maintainers)

Most upvoted comments

It doesn’t work locally, but it works in AWS. So much weird this behavior, as we need a consistent way of delivering things.

Hey AWS, please fix this one.

As a workaround, it seems to work if you add a dummy AWS::Serverless::Api resource with BinaryMediaTypes.

Resources:
  Api:
    Type: AWS::Serverless::Api
    Properties:
      BinaryMediaTypes:
        - application~1octet-stream
      StageName: dev

Tho, I think this behavior is because of a bug of sam-cli.

This continues to be an active issue impeding our development.

Facing the same issue - it works when deployed, but not locally.

Facing the same issue right now

@oleh-kobzar Our team doesn’t (typically) look at closed issues, so creating a new one and relating is the right thing to do.

The issue was closed due to this: https://github.com/aws/aws-sam-cli/issues/1216#issuecomment-880193364. So reading the explanation by my co-worker, it seems like it could be due to how you are calling he endpoint locally. So, how are you executing the endpoint? What is the content_type you are passing?

We see the same problem too, if you try to set it on HttpApi you get:

Error: [InvalidGlobalsSectionException('Globals', "'BinaryMediaTypes' is not a supported property of 'HttpApi'. Must be one of the following values - ['Auth', 'AccessLogSettings', 'StageVariables', 'Tags', 'CorsConfiguration', 'DefaultRouteSettings', 'Domain', 'RouteSettings', 'FailOnWarnings']")] ('Globals', "'BinaryMediaTypes' is not a supported property of 'HttpApi'. Must be one of the following values - ['Auth', 'AccessLogSettings', 'StageVariables', 'Tags', 'CorsConfiguration', 'DefaultRouteSettings', 'Domain', 'RouteSettings', 'FailOnWarnings']")

This is (by its absence) documented Api has it whilst HttpApi does not, but when in production the body is base64 encoded for HttpApi, whilst locally you see:

$ sam local start-api
[snipped]
UnicodeDecodeError while processing HTTP request: 'utf-8' codec can't decode byte 0xff in position 5782: invalid start byte                                                  
2023-05-16 07:15:50 127.0.0.1 - - [16/May/2023 07:15:50] "POST /whatever HTTP/1.1" 502 -

N.B. command generating this was curl -sS -D /dev/stderr -F moo=@moo.tgz http://localhost:3000/whatever

Same problem if you POST using:

curl -sS -D /dev/stderr -H 'content-type: application/octet-stream' --data-binary @moo.tgz http://localhost:3000/whatever