How do I sent both string parameters and a request body on a GET API call?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2022 05:20 PM
Everything works in Postman but I'm not getting this to work in ServiceNow.
If I had the endpoint:
The query parameter:
- code=abc12345
The JSON body:
- { "name" : "helloworld" }
Simple script:
var json = {
"name":"helloworld"
};
var jsonBody = new JSON().encode(json);
var r = new sn_ws.RESTMessageV2('Function', 'GET Name');
r.setStringParameterNoEscape('code', 'abc12345');
r.setRequestBody(jsonBody);
var response = r.execute();
I expect to get response like:
- "Name helloworld is available."
But I get a response like:
- "Name undefined is available."
I don't know why the string parameters were sent successfully (if code is missing, then it would error), but sending the body did not seem to get sent. I have a similar POST API call working completely fine. It seems ServiceNow has an issue with sending a request body in a GET call.
Is there a way to successfully send the example above?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2022 07:58 AM
Are you running this script in scope (not Global)? If so, change
var jsonBody = new JSON().encode(json);
to
var jsonBody = new global.JSON().encode(json);
or use
var jsonBody = encodeURI(JSON.stringify(json));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2022 08:03 AM
But according to this thread, it doesn't event matter, because as you said, body cannot be used with GET method.