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
@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 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.
Any news/suggestions? Our team is somewhat blocked by this issue for two months now :(
Facing the same issue right now
Facing the same issue - it works when deployed, but not locally.
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.
This continues to be an active issue impeding our development.
Still an issue.
Still having the same issue
same issue still
Same issue here, and seems BinaryMediaTypes is not allowed to add in HttpApi?
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
Same problem here! inconsistency creates a lot of confusion!
I'm facing the same issue as @jimdigriz . Any workarounds when using HttpApi events?
Facing the same issue - it works when deployed, but not locally. Any news?