node.js使用require给flume提交请求

 
node.js使用require给flume提交请求 - 简书 https://www.jianshu.com/p/02c20e2d011a
 
 
玄月府的小妖在debug 
2017.04.15 09:22*
 
最后编辑于 2017.12.06 17:44
 字数 43 阅读 120评论 0

flume http接口:

1.使用request模块

npm install request

代码

/********request提交请求*************/
var request = require('request');

var data = [{
    "body":JSON.stringify([{
        "userName": "Abel",
        "singer": "one444477",
        "location": "SH",
        "userTime": 1491808978.90688,
        "gender": "M",
        "age": 27,
        "devName": 1,
        "share": 0,
        "songName": "one",
        "occupation": "IT222222"
    }])
}];



 request({
        url: 'http://222.31.81.214:5140',
        method: "POST",
        json: true,
        headers: {
            "content-type": "application/json",
        },
        body: data
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log('----post--info------', data);
        }else{
            console.log(body);
        }
    });

2.使用request-json提交请求

npm install request-json

代码:

/********request-json提交请求*************/

requestjson = require('request-json');

var client =requestjson.createClient('http://222.31.81.214:5140',{"Content-type":"application/json"});


var data = [{
    "body":JSON.stringify([{
        "userName": "Abel",
        "singer": "one444477",
        "location": "SH",
        "userTime": 1491808978.90688,
        "gender": "M",
        "age": 27,
        "devName": 1,
        "share": 0,
        "songName": "one",
        "occupation": "IT"
    }])
}];

client.post('/', data,function(err, res, body) {
  return console.log(body);
});

注:一定要将提交的json字符串放在body里面

原文地址:https://www.cnblogs.com/rsapaper/p/9889765.html