我使用amazon api网关设置API,并希望将数据放入kinesis流中。 Amazon api网关内置了对它的支持。但是当我试图放入json数据时,它会给出“序列化异常”。
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"
};
在此之后我发出了一个放置请求
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,但根本没有运气。
我无法弄清楚我做错了什么。请帮忙。
答案 0 :(得分:1)
有关如何在http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-kinesis.html的官方AWS文档中在Kinesis前面设置API网关的演练
答案 1 :(得分:0)
您必须使用$ utils变量将其转换为集成请求模板上的base64,这是我的集成请求模板:
#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
。