Dynamic value to Table Rest API Request body

gauravbanga19
Giga Contributor

I am trying to pass html text input value to the rest request body. In the wiki, there is a example of rest call but it's body is having static values.

What i am trying, is pass javascript variable value to json kay., I tried to use FormData() api to construct requestbody but getting error. What's the right way to do pass variable value?

<script>

function postAction(){

var desc = document.getElementById("userinput").value;

  var requestBody = '{"short_description":"Test insert"}';

    var client=new XMLHttpRequest();

  client.open("POST","/api/now/table/incident");

  client.setRequestHeader('Accept','application/json');

  client.setRequestHeader('Content-Type','application/json');

  client.onreadystatechange = function(){

......

  }};

</script>

 

 

9 REPLIES 9

Hi Akash,



I am getting below error with   '{"short_description":'+desc.toString()+'}'


400{"error":{"message":"Exception while reading request","detail":"Cannod decode: {\"short_description\":testing input\n}"},"status":"failure"}



Regards,


Gaurav Banga


You could refer to this wiki article, get a REST message built, sent and validated.


Outbound REST Web Service - ServiceNow Wiki


4.2.2 Testing a Method



Then you could figure out how to send the same message via the UI


Hi Tony,



Thanks Tony for sharing the link.



I have tested the rest message and there I have to pass {"short_description":"${short_description}"}in the content field which doesn't work in UI page:(



Regards,


Gaurav Banga


HI Rajput,



Thanks for your help, I got it working with adding (") before after desc.toString(). And decode error was coming since I was having Enter Key(\n) in my desc text, I have to use variable substitution.


'{"short_description":"'+desc.toString()+'"}';



Regards,


Gaurav Banga


gr8