Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the API Gateway corrupting my binary file?

I have an AWS API Gateway acting as a proxy to a backend service:

{
  "apiKeySource": "HEADER", 
  "name": "-", 
  "createdDate": 1513820260, 
  "binaryMediaTypes": [
      "application/zip", 
      "application/octet-stream"
  ], 
  "endpointConfiguration": {
      "types": [
          "EDGE"
      ]
  }, 
  "id": "-"

}

The integration definition is here:

{
  "integrationResponses": {
      "200": {
          "responseTemplates": {
              "application/json": null
          }, 
          "statusCode": "200"
      }
  }, 
  "passthroughBehavior": "WHEN_NO_MATCH", 
  "timeoutInMillis": 29000, 
  "uri": "http://${stageVariables.backend}:7000/{proxy}", 
  "connectionType": "INTERNET", 
  "httpMethod": "ANY", 
  "cacheNamespace": "iv06s3", 
  "type": "HTTP_PROXY", 
  "requestParameters": {
      "integration.request.path.proxy": "method.request.path.proxy", 
      "integration.request.header.X-Source-IP": "context.identity.sourceIp"
  }, 
  "cacheKeyParameters": [
      "method.request.path.proxy"
  ]
 }

I have an endpoint that generates a Zip file on the fly and returns it to the requester.

When I access the endpoint directly, the file is fine. When I access it via the API Gateway, it gets corrupted.

The corruption takes the form of bytes in the original file being converted to 0xEFBFBD. This is the UTF-8 'replacement character'.

My request has Accept set to application/zip and the response has Content-Type: application/zip.

My expectation is that the API Gateway should recognize this as a binary media type and leave the file alone, but it seems pretty clear that it's processing it as text content.

What am I doing wrong?

like image 546
Dancrumb Avatar asked Aug 27 '18 17:08

Dancrumb


1 Answers

Setting the "Binary Media Type" to "multipart/form-data" resolved a similar issue for me. See here: AWS Api Gateway as a HTTP Proxy is currupting binary uploaded image files

like image 159
Uria W Avatar answered Sep 28 '22 09:09

Uria W