OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Handling an image/jpeg response from Lambda in API Gateway

  • Thread starter Thread starter Explorex
  • Start date Start date
E

Explorex

Guest
I have been at this for hours trying to resolve my issue and I cannot for the life of me.

I have an API that simply fetches an image from another API. All I would like to do is run a GET request and the response be the image.

I am using AWS services Lambda & API Gateway - I have followed every implementation I can see online however it doesn't work in the way I need it to.

I have an endpoint, a GET request that runs a Lambda proxy integration

API Gateway Integration Request

When this is called it then runs my Lambda function which looks as follows. This is a Base64 response, which is how I've read this needs to be handled in Lambda.

Lambda Function

I also read that I needed to specifically add binary media type so API Gateway knows how to handle the response. Now this is the key problem, when I set the binary media type to */* it works as expected, an image is returned in the browser, however this now converts all my API responses to binary which is not ideal as typically they are json - as such, I only want images to be converted. When I use image/jpeg which has been configured as my response body in API Gateway and my response header in Lambda I no longer can see the image. See below example of binary media type configuration enter image description here

See below example of API response how image is viewed in browser when setting binary media type to image/jpeg No image from API

See below example of API response with how image is viewed in browser when setting binary media type to */* (functioning as I'd like) Image from API

and the thing that really throws me off, is when I test my API in postman, with the binary media type set as */* I can see the image, but looking into the response headers I can clearly see its been set to image/jpeg so I can't understand why API Gateway doesn't work when I expect it to. enter image description here
<p>I have been at this for hours trying to resolve my issue and I cannot for the life of me.</p>
<p>I have an API that simply fetches an image from another API. All I would like to do is run a GET request and the response be the image.</p>
<p>I am using AWS services Lambda & API Gateway - I have followed every implementation I can see online however it doesn't work in the way I need it to.</p>
<p>I have an endpoint, a GET request that runs a Lambda proxy integration</p>
<p><a href="https://i.sstatic.net/DGkbyN4E.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/DGkbyN4E.png" alt="API Gateway Integration Request" /></a></p>
<p>When this is called it then runs my Lambda function which looks as follows. This is a Base64 response, which is how I've read this needs to be handled in Lambda.</p>
<p><a href="https://i.sstatic.net/TMr5PkrJ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/TMr5PkrJ.png" alt="Lambda Function" /></a></p>
<p>I also read that I needed to specifically add binary media type so API Gateway knows how to handle the response. Now this is the key problem, when I set the binary media type to <code>*/*</code> it works as expected, an image is returned in the browser, however this now converts all my API responses to binary which is not ideal as typically they are json - as such, I only want images to be converted. When I use <code>image/jpeg</code> which has been configured as my response body in API Gateway and my response header in Lambda I no longer can see the image.
See below example of binary media type configuration
<a href="https://i.sstatic.net/gwzc3xVI.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/gwzc3xVI.png" alt="enter image description here" /></a></p>
<p>See below example of API response how image is viewed in browser when setting binary media type to <code>image/jpeg</code>
<a href="https://i.sstatic.net/QUvNkJnZ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/QUvNkJnZ.png" alt="No image from API" /></a></p>
<p>See below example of API response with how image is viewed in browser when setting binary media type to <code>*/*</code> (functioning as I'd like)
<a href="https://i.sstatic.net/lGZw1dI9.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/lGZw1dI9.png" alt="Image from API" /></a></p>
<p>and the thing that really throws me off, is when I test my API in postman, with the binary media type set as <code>*/*</code> I can see the image, but looking into the response headers I can clearly see its been set to <code>image/jpeg</code> so I can't understand why API Gateway doesn't work when I expect it to.
<a href="https://i.sstatic.net/AJEp5zG8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/AJEp5zG8.png" alt="enter image description here" /></a></p>
Continue reading...
 

Latest posts

Top