Dynamic value to Table Rest API Request body
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-19-2015 06:50 AM
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>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-21-2015 02:54 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-21-2015 03:05 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-21-2015 03:39 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-21-2015 03:43 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-21-2015 04:52 AM
gr8