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

tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Gaurav,



It seems you want to invoke a REST call on one instance with the incident table on another instance, can you confirm?



If the above in broadly correct, I would suggest referring to this wiki article and getting the REST message build and sent as a first step.



Outbound REST Web Service - ServiceNow Wiki


4.2.2 Testing a Method



If you are on Fuji checking out the Rest Explorer might be helpful


REST API Explorer - ServiceNow Wiki



Best Regards



Tony


Hi Tony,



No, I am trying to create ticket on same instance using speech to text google api.



Let me brief you what I am trying:- So i have created a ui page where i am capturing the input from user using google javascript api and using that input, submit a ticket automatically without user clicking on the page.



Where I am stuck is: feeding the user input in client rest call as content dynamically.


******************************************


var desc = document.getElementById("userinput").value           // this giving the user input value


var requestBody = '{"short_description":"Test insert"}';   // i want to use desc variable value as short_description


var client=new XMLHttpRequest();


*******************************************


I have tried using below format but it gives null value for desc variable, may be because it's gets set parsed server side before form is loaded.


var requestBody = '{"short_description":"${desc}"}' -



Regards,


Gaurav Banga


Ah I see what you want to achieve.


Could you assign the value that is intended to be a string representing the short description to a variable and then check this variable contains the short des you are expecting?


Trapping and handling an unexpected result could be good idea at this point in any case.


i.e validate that the google javascript api is working as expected before trying the use the value.



write this code in client side in ui page instead of XML area... or wrap ur code in <script> tag in XML area and use something like this



var requestBody = '{"short_description":'+desc.toString()+'}'



as desc is a javascript variable not a jelly variable.