在node.js中调用restful api

时间:2017-03-07 05:32:09

标签: node.js rest

我的路线中有一个方法,我想调用uri中提到的API。我能够成功调用该方法。但是现在我在我的restful API中创建了一个方法示例,我需要从node.js传递一个值并打印连接的值。

我有一个接受String参数的示例方法。我创建了一个名为variable = paramater的{​​{1}}并将其作为请求发送。但它没有将其合并。< / p>

任何人都可以告诉我在node.js中的restful API中传递值的方法

此处的矿区代码

Hi

1 个答案:

答案 0 :(得分:0)

If we want to use any value which is being passed from UI. We can use it by this way:
 router.post('/restful', function(req, res){
  var platformname=req.body.platform;//This is the way to attach variables from UI.
    var options = {
        uri : 'http://192.168.1.6:8080/sampleRest/RequestxARC/sample',
        body : platformname,
        method : 'post'

            }; 

    console.log(options.body +" value attached from UI");
    var responseFromClient = '';
    request(options,function (error, response, body ,form ,callback) {
        if (!error && response.statusCode == 200) {
            responseFromClient = body;
        }
        else {
            responseFromClient = 'Not Found';
        }
        console.log(responseFromClient);

        //res.json(resss);
        req.flash('response_msg', responseFromClient);

        if(responseFromClient !='Not Found'){
          res.redirect('/users/restful');
        }
        else{
          res.redirect('/users/restful');
        }
    });
 });