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
HttpApitype of event insteand ofApibecause the platform for which I am developing requires lambdas to processAPIGatewayProxyEventV2. 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)
- OS: Microsoft Windows [Version 10.0.19042.1110]
sam --version: 1.26.0- AWS region: local
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 4
- Comments: 17 (2 by maintainers)
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::Apiresource withBinaryMediaTypes.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_typeyou are passing?We see the same problem too, if you try to set it on
HttpApiyou get:This is (by its absence) documented
Apihas it whilstHttpApidoes not, but when in production the body is base64 encoded forHttpApi, whilst locally you see:N.B. command generating this was
curl -sS -D /dev/stderr -F moo=@moo.tgz http://localhost:3000/whateverSame problem if you POST using: