Reputation: 620
I am using AWS API Gateway to consume an image file to send to a Lambda function. The POST reaches my Lambda function just fine, with the binary data transformed into a base64 string, but when I try to decode that base64 string and save it as an image, the image cannot be decoded. In fact, the string returned after transformation is smaller than the image itself, which makes me think that something is going wrong. I have tried simply returning the string provided to the Lambda function as the response and decoding that locally, which doesn't work either.
Here is the configuration for my API Gateway method:
"/photo/{photo_id}": {
"x-amazon-apigateway-any-method": {
"consumes": [
"image/jpeg",
"image/jpg"
],
"produces": [
"application/json",
"image/jpg"
],
"parameters": [
{
"name": "Content-Type",
"in": "header",
"required": false,
"type": "string"
},
{
"name": "Accept",
"in": "header",
"required": false,
"type": "string"
},
{
"name": "photo_id",
"in": "path",
"required": true,
"type": "string"
},
{
"in": "body",
"name": "Empty",
"required": true,
"schema": {
"$ref": "#/definitions/Empty"
}
},
{
"in": "body",
"name": "Empty",
"required": true,
"schema": {
"$ref": "#/definitions/Empty"
}
}
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
}
},
"500": {
"description": "500 response"
}
},
"x-amazon-apigateway-integration": {
"credentials": "redacted",
"uri": "redacted",
"responses": {
"default": {
"statusCode": "200"
},
".*Error.*": {
"statusCode": "500"
}
},
"requestParameters": {
"integration.request.header.Accept": "method.request.header.Accept",
"integration.request.header.Content-Type": "method.request.header.Content-Type"
},
"requestTemplates": {
"image/jpeg": "{\"photo\": \"$input.body\", \"photo_id\": \"$input.params('photo_id')\", \"app_id\": \"$input.params('x-app-id')\", \"httpMethod\": \"$context.httpMethod\"}",
"image/jpg": "{\"photo\": \"$input.body\", \"photo_id\": \"$input.params('photo_id')\", \"app_id\": \"$input.params('x-app-id')\", \"httpMethod\": \"$context.httpMethod\"}"
},
"passthroughBehavior": "when_no_templates",
"httpMethod": "POST",
"contentHandling": "CONVERT_TO_TEXT",
"type": "aws"
}
}
}
And the simple Lambda function to return the string back:
def lambda_handler(event, context):
photo_string = event['photo']
return photo_string
The cURL command I'm using to test the implementation:
curl -X POST https://execute-api.eu-central-1.amazonaws.com/prod/photo/new -H "Content-Type: image/jpg" -H "x-app-id:test" -d @IMG_1642.jpg
Any idea why the base64 that is returned would be different?
Upvotes: 1
Views: 1296
Reputation: 620
I figured it out, I needed to be using --data-binary
in my cURL invocation instead of just -d
which is an alias for --data-ascii
. It works fine now.
Upvotes: 1