<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anurag R Naik</title>
    <description>The latest articles on DEV Community by Anurag R Naik (@anuragrnaik).</description>
    <link>https://dev.to/anuragrnaik</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F683727%2Fb2f51bdd-79e1-4b9a-a606-9b36aae51c5d.jpeg</url>
      <title>DEV Community: Anurag R Naik</title>
      <link>https://dev.to/anuragrnaik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anuragrnaik"/>
    <language>en</language>
    <item>
      <title>Enabling Binary response from AWS API Gateway endpoint with Lambda Proxy integration</title>
      <dc:creator>Anurag R Naik</dc:creator>
      <pubDate>Thu, 12 Aug 2021 14:59:46 +0000</pubDate>
      <link>https://dev.to/anuragrnaik/enabling-binary-response-from-aws-api-gateway-endpoint-with-lambda-proxy-integration-1kj0</link>
      <guid>https://dev.to/anuragrnaik/enabling-binary-response-from-aws-api-gateway-endpoint-with-lambda-proxy-integration-1kj0</guid>
      <description>&lt;p&gt;AWS API Gateway is a fully-managed service that helps you create, publish and manage RESTful APIs and WebSocket APIs with a significant level of scale-out/ scale-in at your disposal. AWS provides two possible integrations with AWS Lambda which is a FaaS (Function as a Service) to handle your business logic.&lt;br&gt;
Lambda-Proxy is a simple integration which forwards the request/response without any mutations, whereas a Lambda integration is complex and enables us to customize the request/response based on request/response templates provided in the API Gateway integration.&lt;/p&gt;

&lt;p&gt;This post describes how we can enable an API Gateway endpoint to return binary response (viz. pdf, image, etc.) which has a Lambda Proxy Integration.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frp1tj60bjkgwja4iq7u8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frp1tj60bjkgwja4iq7u8.png" alt="API Gateway returning a file/ binary response from proxy-integrated lambda "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enabling binary response in a Lambda-Proxy integration is a bit different from the conventional non-proxy based Lambda integration.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One can refer the following documents for the conventional Lambda request integration with API Gateway with Binary support enabled, check &lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html" rel="noopener noreferrer"&gt;Non-proxy integrations&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also take into consideration the available &lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#http-api-quotas" rel="noopener noreferrer"&gt;API Gateway quota limitations&lt;/a&gt; if you are planning to return a file blob.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Steps for getting binary content response from API Gateway with lambda proxy integration:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Unlike the conventional Lambda binary integration, proxy-integrated Lambda response requires explicit conversion of the binary data to a base64 encoded string before initiating the lambda callback. "isBase64Encoded" flag is also set in the response. The base64 encoded string is the only data added to the body of the response object.&lt;/li&gt;
&lt;/ul&gt;

&lt;h6&gt;
  
  
  The below code-snippet exhibits the ideal response returned by the lambda function (javascript):
&lt;/h6&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

    &lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;application/pdf&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// content-type may vary depending on the document to be sent&lt;/span&gt;
          &lt;span class="p"&gt;...,&lt;/span&gt;
          &lt;span class="p"&gt;...,&lt;/span&gt;
          &lt;span class="p"&gt;...&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;base64EncodedDocString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;isBase64Encoded&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;As the 'content-type' and 'accept' HTTP headers are referred by the API Gateway to decide how to handle the body, it is ideal not to fix the 'content-type' header to the binary type on response and to change it in case of a lambda execution exception or a different 'accept' header sent in the initial request&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;The API Gateway converts the base64 string from the response to binary, and to ensure this, the required media type is to be added in the &lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-configure-with-console.html" rel="noopener noreferrer"&gt;API Gateway Settings&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can add the Binary Media Types via aws-cli as well:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;p&gt;aws apigateway update-rest-api — rest-api-id XXX — patch-operations &lt;br&gt;
‘[&lt;span class="o"&gt;{&lt;/span&gt;“op” : “replace”, “path” : “/binaryMediaTypes/&lt;span class="k"&gt;&lt;em&gt;&lt;/em&gt;&lt;/span&gt;~1&lt;span class="k"&gt;&lt;/span&gt;”, “value” : “&lt;span class="k"&gt;&lt;em&gt;&lt;/em&gt;&lt;/span&gt;~1&lt;span class="k"&gt;&lt;/span&gt;”&lt;span class="o"&gt;}]&lt;/span&gt;’&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  References:&lt;br&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/lambda-proxy-binary-media.html" rel="noopener noreferrer"&gt;Return binary media from a Lambda proxy integration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-paylapi%20oad-encodings-configure-with-console.html" rel="noopener noreferrer"&gt;Enabling binary support using the API Gateway console&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-paylapi%20oad-encodings-configure-with-console.html" rel="noopener noreferrer"&gt;Available Content type conversions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>aws</category>
      <category>lambda</category>
      <category>apigateway</category>
    </item>
  </channel>
</rss>
