如何使用Amazon API网关将json数据放入Kinesis流 [英] how to Put json data using Amazon API gateway to Kinesis stream

查看:77
本文介绍了如何使用Amazon API网关将json数据放入Kinesis流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Amazon api网关设置了API,并希望将数据放入运动流.Amazon api网关对此具有内置支持.但是,当我尝试放入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"
    };

此后,我发出放置请求

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.

这篇关于如何使用Amazon API网关将json数据放入Kinesis流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆