众所周知,API Gateway和lambda支持二进制请求/响应,但我对节点JavaScript中的后端编程有一个问题。
环境:
在上述环境中,在我的代码中,我将响应内容作为二进制(缓冲对象数组) 但是,如果我直接将Buffer对象数组作为响应,
callback(null,{
headers: {'Content-Type': 'image/jpeg'},
body: body
});
接收回复是这样的:
Content-type: image/jpeg
{type=Buffer, data=[255,216,255,224,0,16,74,70,73,70,0...
如果我通过base64编码将缓冲区对象数组作为响应,
callback(null,{
headers: {'Content-Type': 'image/jpeg'},
body: body.toString('base64')
});
接收回复是这样的:
Content-type: image/jpeg
/9j/4AAQSkZJRgABAQEASABIAAD/2wBDA...
如何使用无服务器框架从节点JS后端向API网关提供二进制响应?
== PostScript ==
根据该文件:
AWS API Gateway Binary output without Accept header
我们必须设置内容处理"积分响应更改为" CONVERT TO BINARY",用于响应二进制响应
但是我该如何设置呢?
我不知道serverless.yml和AWS控制台GUI。
如果我成功设置了此内容处理=> CONVERT TO BINARY,我可以解决响应二进制响应吗?
== 1月17日编辑==
嗨@ ka-hou-ieong
你写了rest-api-id和resource-id,它们在下面的图片中,对吗?
但是使用这些id,命令结果说:
$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
这有什么问题?我使用最新的aws-cli(aws-cli / 1.11.37 Python / 2.7.9 Darwin / 16.3.0 botocore / 1.5.0)
答案 0 :(得分:3)
如果您想强制将响应作为二进制响应,则可以设置' CONVERT_TO_BINARY'通过AWS CLI或API通过contentHandling进行集成响应。目前,我们在控制台上缺少此选项。
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