Bug: Error with binary files sent through api
Description:
When sending file (postman or frontend) to backend lambda through API Gateway I'm getting an error while doing local testing with sam local start-api
UnicodeDecodeError while processing HTTP request: 'utf-8' codec can't decode byte 0xb5 in position 473: invalid start byte.
While the deployed application behave normally with an actual API Gateway and Lambda
Steps to reproduce:
template:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
GAPP-debug
Globals:
Function:
Timeout: 360
Resources:
gappbeapi:
Type: AWS::Serverless::HttpApi
Properties:
CorsConfiguration:
AllowOrigins:
- "*"
AllowMethods:
- GET
- POST
- OPTIONS
AllowHeaders:
- Content-Type
- Accept
- Access-Control-Allow-Headers
- Access-Control-Request-Method
- Access-Control-Request-Headers
- Authorization
gappbe:
Type: AWS::Serverless::Function
Properties:
CodeUri: fulfillment/ful_deployment_package.zip
Handler: index.lambda_handler
Runtime: python3.13
Architectures:
- x86_64
Events:
HelloWorld:
Type: HttpApi
Properties:
Path: /sendData
Method: POST
ApiId: !Ref gappbeapi
Policies:
- Statement:
- Sid: lambda
Effect: Allow
Action:
- lambda:InvokeFunction
Resource: arn:aws:lambda:us-east-1:*:function:*
python code example:
# index.py
def lambda_handler(event, context):
print("do things here")
try:
if not event:
raise ValueError("Empty event object")
return "All good"
except Exception as e:
return "Bad response"
curl:
curl --location 'http://127.0.0.1:3000/sendData' \
--form 'cxName="postman"' \
--form 'email="emailpost"' \
--form 'vafile=@"/Users/xxxx/Downloads/somefile.pdf"'
Observed result:
○ → sam local start-api --debug
2025-03-10 09:20:34,406 | No config file found in this directory.
2025-03-10 09:20:34,408 | OSError occurred while reading TOML file: [Errno 2] No such file or directory: '/Users/mjkubba/workspace/debug/samconfig.toml'
2025-03-10 09:20:34,409 | Config file location: /Users/mjkubba/workspace/debug/samconfig.toml
2025-03-10 09:20:34,410 | Config file '/Users/mjkubba/workspace/debug/samconfig.toml' does not exist
2025-03-10 09:20:34,412 | Using SAM Template at /Users/mjkubba/workspace/debug/template.yaml
2025-03-10 09:20:34,430 | OSError occurred while reading TOML file: [Errno 2] No such file or directory: '/Users/mjkubba/workspace/debug/samconfig.toml'
2025-03-10 09:20:34,431 | Using config file: samconfig.toml, config environment: default
2025-03-10 09:20:34,431 | Expand command line arguments to:
2025-03-10 09:20:34,431 | --template_file=/Users/mjkubba/workspace/debug/template.yaml --host=127.0.0.1 --port=3000 --static_dir=public
--layer_cache_basedir=/Users/mjkubba/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1
2025-03-10 09:20:34,492 | local start-api command is called
2025-03-10 09:20:34,726 | No Parameters detected in the template
2025-03-10 09:20:34,745 | There is no customer defined id or cdk path defined for resource gappbeapi, so we will use the resource logical id as the resource id
2025-03-10 09:20:34,745 | There is no customer defined id or cdk path defined for resource gappbe, so we will use the resource logical id as the resource id
2025-03-10 09:20:34,746 | 0 stacks found in the template
2025-03-10 09:20:34,746 | No Parameters detected in the template
2025-03-10 09:20:34,756 | There is no customer defined id or cdk path defined for resource gappbeapi, so we will use the resource logical id as the resource id
2025-03-10 09:20:34,756 | There is no customer defined id or cdk path defined for resource gappbe, so we will use the resource logical id as the resource id
2025-03-10 09:20:34,757 | 2 resources found in the stack
2025-03-10 09:20:34,757 | Found Serverless function with name='gappbe' and CodeUri='fulfillment/ful_deployment_package.zip'
2025-03-10 09:20:34,758 | --base-dir is not presented, adjusting uri fulfillment/ful_deployment_package.zip relative to /Users/mjkubba/workspace/debug/template.yaml
2025-03-10 09:20:34,758 | Skip building zip function: gappbe
2025-03-10 09:20:36,121 | Detected Inline Swagger definition
2025-03-10 09:20:36,123 | Parsing Swagger document using 3.0 specification
2025-03-10 09:20:36,123 | Found '0' authorizers in resource 'gappbeapi'
2025-03-10 09:20:36,124 | Lambda function integration not found in Swagger document at path='/sendData' method='post'
2025-03-10 09:20:36,124 | Found '0' APIs in resource 'gappbeapi'
2025-03-10 09:20:36,124 | authorizer not found or disabled, returning early
2025-03-10 09:20:36,125 | Found '1' API Events in Serverless function with name 'gappbe'
2025-03-10 09:20:36,125 | Removed duplicates from '1' Explicit APIs and '0' Implicit APIs to produce '1' APIs
2025-03-10 09:20:36,125 | 1 APIs found in the template
2025-03-10 09:20:36,129 | Mounting gappbe at http://127.0.0.1:3000/sendData [POST, OPTIONS]
2025-03-10 09:20:36,129 | You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will
be reflected instantly/automatically. If you used sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only need to restart
SAM CLI if you update your AWS SAM template
2025-03-10 09:20:36,130 | Localhost server is starting up. Multi-threading = True
2025-03-10 09:20:36,130 | Setting SIGTERM interrupt handler
2025-03-10 09:20:36 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:3000
2025-03-10 09:20:36 Press CTRL+C to quit
2025-03-10 09:20:41,076 | UnicodeDecodeError while processing HTTP request: 'utf-8' codec can't decode byte 0xb5 in position 473: invalid start byte
2025-03-10 09:20:41,077 | Lambda execution failed ()
2025-03-10 09:20:41 127.0.0.1 - - [10/Mar/2025 09:20:41] "POST /sendData HTTP/1.1" 502 -
Expected result:
passing in the file as binary to lambda for processing
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
○ → sam --info
{
"version": "1.133.0",
"system": {
"python": "3.13.2",
"os": "macOS-14.7.4-arm64-arm-64bit-Mach-O"
},
"additional_dependencies": {
"docker_engine": "27.3.1",
"aws_cdk": "2.44.0 (build bf32cb1)",
"terraform": "1.10.5"
},
"available_beta_feature_env_vars": [
"SAM_CLI_BETA_FEATURES",
"SAM_CLI_BETA_BUILD_PERFORMANCE",
"SAM_CLI_BETA_TERRAFORM_SUPPORT",
"SAM_CLI_BETA_RUST_CARGO_LAMBDA"
]
}
- OS: macOS
sam --version: 1.133.0- AWS region: us-east-1
I'm seeing similar issues; though using
Runtime: nodejs20.x
The issue happens in event_constructor.py #137 when SAM tries to decode request contains file in pdf or others format.
Also seeing this. Is there any known workaround?