The Zurich release has arrived! Interested in new features and functionalities? Click here for more

POST request to servicenow from NodeJS

nahidalam
Kilo Contributor

Hi

I am trying to create a new ticket to servicenow by sending a POST request from my NodeJS application.

Initially I wrote the below code:

var request = require('request');

request.post(

      'https://MYINSTANCE.service-now.com/api/now/v1/table/incident',

      { json: { key: 'value' } },

      function (error, response, body) {

              if (!error && response.statusCode == 200) {

                      console.log(body)

              }

else {

console.log("error")

}

      }

);

This returns an html instead of a JSON. What am I missing?

1 ACCEPTED SOLUTION

robertchumley
Giga Contributor

Here is an example that pulls from the incident table.   I typically use axios instead of request for various reasons. There is also an API explorer to help you form your requests in a better way.



import axios from 'axios';



axios.get(


      'https://{your instance}.service-now.com/api/now/v2/table/incident?sysparm_limit=1',


      {headers:{"Accept":"application/json","Content-Type":"application/json","Authorization": ("Basic " + new Buffer("admin:{your password}").toString('base64'))}}


).then((data)=>{


      console.log("Your Data",data)


}).catch((e)=>{


      console.log("error",e.toString());


});


View solution in original post

3 REPLIES 3

robertchumley
Giga Contributor

Here is an example that pulls from the incident table.   I typically use axios instead of request for various reasons. There is also an API explorer to help you form your requests in a better way.



import axios from 'axios';



axios.get(


      'https://{your instance}.service-now.com/api/now/v2/table/incident?sysparm_limit=1',


      {headers:{"Accept":"application/json","Content-Type":"application/json","Authorization": ("Basic " + new Buffer("admin:{your password}").toString('base64'))}}


).then((data)=>{


      console.log("Your Data",data)


}).catch((e)=>{


      console.log("error",e.toString());


});


awessel
Kilo Guru

This seems to be more node.js related than SN related. Not sure why you're getting back HTML but I'm pretty sure that your request.post call is not formed correctly. Typically with these request functions the first argument you supply is a json object with the url, headers and body. You would also need to supply authentication data since you're looking to create records. Also, be sure you're including an "Accept" header set to "application/json" to ensure the response you get back is formatted as a json object.


venkateshchalla
Kilo Contributor

Here is an example to create incident using Request module in NodeJS



var request = require('request');



var options = {


      url: 'https://[MY INSTANCE].service-now.com/api/now/table/incident',


      method: 'POST',


      headers: {"Accept":"application/json","Content-Type":"application/json","Authorization": ("Basic " + new Buffer("[USERNAME]:[PASSWORD]").toString('base64'))},


      json: true,


      body: {'short_description':'Creating incident through Request','assignment_group':'287ebd7da9fe198100f92cc8d1d2154e','urgency':'3','impact':'3'}


};



function callback(error, response, body) {


   


  if (error) {


      console.log(error);


  } else{


          console.log(body);


  }


}



request(options,callback);