As we know, API Gateway and lambda support binary request/response, but I have one question for backend programing in node JavaScript.
Environment:
With above environments, in my code I have response content as Binary (Buffer objects array).
But, if I give Buffer objects array as response directly,
callback(null,{
headers: {'Content-Type': 'image/jpeg'},
body: body
});
Receiving response is like this:
Content-type: image/jpeg
{type=Buffer, data=[255,216,255,224,0,16,74,70,73,70,0...
If I give Buffer objects array as response by base64 encoded,
callback(null,{
headers: {'Content-Type': 'image/jpeg'},
body: body.toString('base64')
});
Receiving response is like this:
Content-type: image/jpeg
/9j/4AAQSkZJRgABAQEASABIAAD/2wBDA...
How can I give binary response to API Gateway from node JS backend using Serverless framework?
== PostScript ==
According to this document:
AWS API Gateway Binary output without Accept header
We must set "Content Handling" of Integration response change to "CONVERT TO BINARY", for responding binary response.
But how can I set this?
I have no idea both from serverless.yml and AWS console GUI.
And if I successfully set this Content Handling => CONVERT TO BINARY, might I solve responding binary response?
== Edited Jan. 17th ==
Hi @ka-hou-ieong
You wrote rest-api-id and resource-id, they are in below images, right?
But using these ids, command result said:
$aws apigateway put-integration-response --rest-api-id XXXXXXXX --resource-id XXXXXX --http-method GET --status-code 200 --content-handling CONVERT_TO_BINARY
An error occurred (NotFoundException) when calling the PutIntegrationResponse operation: Invalid Resource identifier specified
What wrong with this? I use latest aws-cli (aws-cli/1.11.37 Python/2.7.9 Darwin/16.3.0 botocore/1.5.0)
Under the selected API in the primary navigation panel, choose Settings. In the Settings pane, choose Add Binary Media Type in the Binary Media Types section. Type a required media type, for example, image/png , in the input text field. If needed, repeat this step to add more media types.
API Gateway supports containerized and serverless workloads, as well as web applications.
If you want to force the response as a binary response, you can set 'CONVERT_TO_BINARY' to the contentHandling on integration response via AWS CLI or via API. Currently, we are lack of this option on the console.
PATCH /restapis/<restapi_id>/resources/<resource_id>/methods/<http_method>/integration/responses/<status_code>
{
"patchOperations" : [ {
"op" : "replace",
"path" : "/contentEncoding",
"value" : "CONVERT_TO_BINARY"
}]
}
aws apigateway put-integration-response --rest-api-id xxxxxxx --resource-id xxxxx --http-method GET --status-code 200 --content-handling CONVERT_TO_BINARY
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With