1

Hi I am new to this domain, I am trying to do PUT request and add the json file. I have the json file created and i have to perform put and post request using the URI's please can any one post a code using nodejs and it will be helpful.I created a put request file like this

var i = 0;
var fs = require("fs");
var request = require('request');
var jsonPath =     fs.readFileSync('filepath');
// String --> Object
var jsonObj = JSON.parse(jsonPath);
console.log(changedevicename.call());
for( i = 0; i < jsonObj.ipConfig.length; i++) 
{
   var ipv4URI = jsonObj.ipConfig[i].ipv4;                                           // taking ipv4 json file
   var ipv6URI = jsonObj.ipConfig[i].ipv6;                                       // taking ipv6 json file
   console.log(ipv4URI);
   console.log(ipv6URI);

   rest_service();
   //console.log(config[i]);
}

function rest_service()                                                        // should I change this or what  
{
    var i = 0;
    var request = require('request');

    var options =   {
       url: 'http://'+'USERNAME'+':'+'PASSWORD'+ '@'+'IPV6'+'URI',              
       method: 'PUT',
    } 
{
    //IP = userGivenIP;
    //IP = '192.168.0.1';        

       request( 
              {
               method:'PUT',
               url: 'http://'+'USERNAME'+':'+'PASSWORD'+ '@'+'IPV6'+'URI',            // 
               headers: {
                         'Content-Type': 'application/json',                       // check this, I should change this
                        },
                        //var ip4Json = JSON.parse(body);                        // check this, I should change this
                        //console.log('\n\n'+ body + '\n\n');
          },     
               function (error, response, body)                        // check this, I should change this
         {
              if (error!=undefined)
              {
              console.log(body);
              }
              else
              {
              console.log("printerror", error);
              console.log("IP disabled");
              }


         });
}


This code has to be doen dynamically but I am not getting how to do this for put and post request please help me out and mail the code thanks.

thanks and regards Prathamesh

1 Answer 1

1

You can add a body parameter to request.

const jsonBody = {
    key1: value1,
    key2: value2
};

const headers = {
    authorization: "<token>"
};

const options = {
        method: 'PUT',
        uri: "some-url",
        headers: headers,   // headers if your api requires
        body: jsonBody,
        json: true
};

request(options, function(err, response) {
     // handle err first
     // do  stuff with response
});

You should go through the docs: https://www.npmjs.com/package/request

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.