美文网首页Node.jsNode.js
node.js使用require给flume提交请求

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

作者: 玄月府的小妖在debug | 来源:发表于2017-04-15 09:22 被阅读86次

    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里面

    相关文章

      网友评论

        本文标题:node.js使用require给flume提交请求

        本文链接:https://www.haomeiwen.com/subject/cjlnattx.html