How can i pass key/values in setRequestBody

Fabrizio Joaqui
Mega Guru

I have to pass these 3 key/values:

FabrizioJoaqui_0-1671616654425.png

 

 i tried to do this:

 

var body = {
client_id: 'SNIT0da4-b027-4a81-a4e7-c6352a315759',
client_secret: '3K72Yv6F7_zLS7zZZc4LW3wHsG4s9Vc1XO7rSfkM',
grant_type: 'client_credentials'
};
var toSend = JSON.stringify(body)
//restMessage......
setRequestBody(toSend)

but it doesn't work

1 ACCEPTED SOLUTION

@Fabrizio Joaqui Please remove the query params you added and try below in script itself. It should work 100%.

 

var body = {
"client_id": 'SNIT0da4-b027-4a81-a4e7-c6352a315759',
"client_secret": '3K72Yv6F7_zLS7zZZc4LW3wHsG4s9Vc1XO7rSfkM',
"grant_type": 'client_credentials'
};

var formBody = [];
for (var property in body) {
  var encodedKey = encodeURIComponent(property);
  var encodedValue = encodeURIComponent(body[property]);
  formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");

r.setRequestBody(formBody);
//r.setRequestBody(JSON.stringify(formBody)); //Try this if above line does not work
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

11 REPLIES 11

i tried to pass as queryStringParameters ($clienti_id etc.....)but it still doesn 't work, 

@Fabrizio Joaqui Please remove the query params you added and try below in script itself. It should work 100%.

 

var body = {
"client_id": 'SNIT0da4-b027-4a81-a4e7-c6352a315759',
"client_secret": '3K72Yv6F7_zLS7zZZc4LW3wHsG4s9Vc1XO7rSfkM',
"grant_type": 'client_credentials'
};

var formBody = [];
for (var property in body) {
  var encodedKey = encodeURIComponent(property);
  var encodedValue = encodeURIComponent(body[property]);
  formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");

r.setRequestBody(formBody);
//r.setRequestBody(JSON.stringify(formBody)); //Try this if above line does not work
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023