简体   繁体   中英

invoke restful api in node.js

I have a method in my routes and I want to invoke the API mentioned in the uri. I am able to invoke the method successfully.But now I have created a method sample in my restful API in which I need to pass a value from node.js and print the concatenated value.

I have the sample method which accepts a String argument.I have created a variable named paramater = Hi and send this as an request.But it is not concatinating it.

Can anyone tell me the way to pass values in restful API in node.js

Here's mine code

router.post('/restful', function (req, res) {
    var options = {
        uri: 'http://192.168.1.6:8080/sampleRest/RequestxARC/sample',
        method: 'post'

    };
    var parameters = "Hi";

    var responseFromClient = '';
    request(options, function (error, response, body, parameters) {
        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');
        }
    });
});
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');
        }
    });
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM