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

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

Open oleh-kobzar opened this issue 4 years ago • 18 comments

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

oleh-kobzar avatar Jul 23 '21 00:07 oleh-kobzar

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

jfuss avatar Aug 06 '21 20:08 jfuss

@jfuss I've provided a snippet of code which is used for testing this lambda locally (in the "Steps to reproduce" section of the original post). It uses FormData with two files applied from Blobs. Then the request is submitted via fetch. By default the content-type is multipart/form-data. This snippet is used via Chrome's Dev Tools.

I tried several different approaches, but nothing helped. Either there is 502 or other issues (don't remember them now, anyway those were for indirect approaches).

But if I remove body at all or supply just a simple JSON then it passes correctly and my labmda start executing.

oleh-kobzar avatar Aug 09 '21 07:08 oleh-kobzar

Any news/suggestions? Our team is somewhat blocked by this issue for two months now :(

oleh-kobzar avatar Sep 28 '21 08:09 oleh-kobzar

Facing the same issue right now

gbordoni avatar Nov 15 '21 14:11 gbordoni

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

Przeszaf avatar Feb 13 '22 20:02 Przeszaf

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.

willianfalbo avatar Apr 13 '22 19:04 willianfalbo

This continues to be an active issue impeding our development.

Brokkoly avatar Jun 22 '22 20:06 Brokkoly

Still an issue.

pvorona avatar Jun 27 '22 23:06 pvorona

Still having the same issue

shanwije avatar Jul 19 '22 06:07 shanwije

same issue still

benjicarpy19 avatar Jan 20 '23 00:01 benjicarpy19

Same issue here, and seems BinaryMediaTypes is not allowed to add in HttpApi?

brewaiMing avatar Mar 14 '23 00:03 brewaiMing

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 [email protected] 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

jimdigriz avatar May 16 '23 06:05 jimdigriz

Same problem here! inconsistency creates a lot of confusion!

peyman-se avatar Dec 30 '23 06:12 peyman-se

I'm facing the same issue as @jimdigriz . Any workarounds when using HttpApi events?

hkaiser25 avatar Jan 17 '24 20:01 hkaiser25

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

svenemtell avatar Jun 11 '24 11:06 svenemtell