如何使用 Amazon API 网关将 json 数据放入 Kinesis 流
我使用 amazon api gateway 设置了一个 API,并希望将数据放入 kinesis 流中.Amazon api gateway 内置了对它的支持.但是当我尝试放置 json 数据时,它给出了序列化异常".
I setup an API using amazon api gateway and want to put data into kinesis streams. Amazon api gateway has inbuilt support for it. But when i trying to put json data it gives "Serialization exception".
var data = {"ua_platform":"iPhone","ua_browsercodename":"Mozilla","ua_browserlanguage":"en-us","ua_header":"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1","ua_browsername":"Netscape","key":"livestream_hindi",,"datetime_ut":"1458711871","datetime_dt":"2016-03-23","value":"15","source":"0","browser":"Mobile Safari-9.0.","os":"iOS-9.1.","device_detail":"iPhone Apple iPhone"};
var json = JSON.stringify(data);
var params = { 'ContentType' : 'application/json','Access-Control-Allow-Headers' : 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'};
var body = {"Data":json,
"StreamName":"XXXXXX",
"PartitionKey":"XXXX"
};
在此之后我提出放置请求
After this i make a put request
apigClient.functionPut(params, body, additionalParams)
.then(function(result){
//This is where you would put a success callback
console.log("success");
}).catch( function(result){
//This is where you would put an error callback
console.log("catch");
});
api 给出了 200 ok 以及序列化异常.在 put 请求中声明,body 变量中的Data"键将只接受blob"类型.现在我也尝试将 JSON 数据转换为 BLOB,但完全没有运气.
The api gives 200 ok along with the serialization exception. It is stated in put request the "Data" key in body variable will accept only "blob" type. Now i also tried converting JSON data to BLOB but no luck at all.
我无法弄清楚我做错了什么.请帮忙.
I am not able to figure out what i am doing wrong. Please help.
您必须在集成请求模板上使用 $utils 变量转换为 base64,这是我的集成请求模板:
You must use the $utils variable to convert to base64 on your integration request template, here's my integration request template:
#set($msgBody = $util.parseJson($input.body))
#set($msgId = $msgBody.messageId)
{
"Data": "$util.base64Encode($input.body)",
"PartitionKey": "$msgId",
"StreamName": "arena-hub-dev-ks"
}
请参阅我正在使用 $util.base64Encode 的带有数据"的行.在这种情况下,我的 APIGW 端点接受 text/plain
.
See the line with the "Data" I'm using $util.base64Encode. In this case my APIGW endpoint accepts text/plain
.