How do I sent both string parameters and a request body on a GET API call?

Jamy1
Tera Guru

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?

  •  
2 REPLIES 2

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

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));

 

But according to this thread, it doesn't event matter, because as you said, body cannot be used with GET method.