3

I tried creating an AWS API Gateway proxy which will pass any HTTP verbs as 'GET' to the target server (because it was needed for specific purpose).

Text and JSON files are transfered without problem, but all images are broken.

It seems that API Gateway proxy somehow intercepts and damages binary/image data.

Is there some specific configuration to allow me pass images also?

DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
Christian
  • 31
  • 2

3 Answers3

1

This page will help: Enabling binary support using the API Gateway console : https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-configure-with-console.html

Frank
  • 1,315
  • 7
  • 24
  • 43
  • I added \*/* to the Binary Media Types in the settings, which fixed my problems. – Austin Taylor Jan 21 '21 at 00:57
  • Just a quick note for above, adding `*/*` to BinaryMediaTypes will cause all requests to be treated as binary data. I had an issue when sending `Content-Type: application/json` in the browser and needed to `json.stringify()` my body otherwise the request would fail. – Karl Taylor Apr 28 '21 at 20:20
0

You have to add the MIME-Types that you want to be treated as binary to the list of Binary Media Types under your API's Settings.

You will also have to ensure that your responses content-type as well as your requests accept header are not in conflict.

If contentHandling is not defined, and if the Content-Type header of the response and the Accept header of the original request match an entry of the binaryMediaTypes list, API Gateway passes through the body. This occurs when the Content-Type header and the Accept header are the same; otherwise, API Gateway converts the response body to the type specified in the Accept header.

excerpt from the API Gateways docs

fschaper
  • 721
  • 7
  • 10
-1

To download an image file (image.jpg) as a binary blob from Lambda:

GET /v1/s3?key=image.jpg HTTP/1.1 Host: abcdefghi.execute-api.us-east-1.amazonaws.com Content-Type: application/json Accept: application/octet-stream

To download an image file (image.jpg) as a Base64-encoded string, formatted as a JSON property, from Lambda:

GET /v1/s3?key=image.jpg HTTP/1.1 Host: abcdefghi.execute-api.us-east-1.amazonaws.com Content-Type: application/json Accept: application/json

Also have look at this AWS Blog Post

Kush Vyas
  • 5,813
  • 2
  • 26
  • 36
  • I would like to use Api Gateway HTTP/AWS Service Proxy instead of lambda and serve directly to web browser – Christian Dec 12 '17 at 09:22
  • 1
    If you want to use directly on a web browser, you can set `image/*` to the binaryMediaTypes. It should cover the accept header on the request from the browser. – Ka Hou Ieong Dec 14 '17 at 00:18