Barry King
Barry King

Reputation: 693

How to return an empty image using AWS API Gateway alone

I would like to return content-type image from AWS API Gateway without the use of a lambda. e.g. so I can reference in on a page.

The returning type needs to be an image content type with no image.

I see that AWS have now implemented binary support but I have been unable to do the above.

Upvotes: 0

Views: 593

Answers (1)

Barry King
Barry King

Reputation: 693

I managed to work this one out.

You might be able to do have less steps but this is what I ended up with.

  1. Create a model for content-type image/png with a body of {} (I called it emptyimage)
  2. Create a GET method.
  3. Integration request: This is just a MOCK; no need to change anything else.
  4. Integration response
    • Remove the 200
    • Add 2\d{2} regex with a method response status of 204
    • Add response header of content-type
  5. Method response.
    • Remove 200
    • Add 204
    • Response headers content-type
    • content-type response set to emptyimage model (created previously)

Calling the API returns a valid 204 no content response for content-type image/png.

If you render this pixel by using javascript you avoid an img tag being required in the dom and you dont get a browser missing image placeholder.

e.g.

<script type="text/javascript"> (new Image()).src = "{url}"; </script> 

Upvotes: 1

Related Questions