1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.