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

jaheerhattiwale
Mega Sage
Mega Sage

@Fabrizio Joaqui Can you please post the screen shot of full code?

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

var token;
var body = {
	client_id: 'SNIT0da4-b027-4a81-a4e7-c6352a315759',
	client_secret: '3K72Yv6F7_zLS7zZZc4LW3wHsG4s9Vc1XO7rSfkM',
	grant_type: 'client_credentials'
};
var toSend = JSON.stringify(body);
	gs.info(toSend);
	var r = new sn_ws.RESTMessageV2('Frida.api.dev','POSTtoken');
	r.setRequestBody(toSend);
        var response = r.execute();
        var responseBody = JSON.parse(response.getBody());
        var httpStatus = response.getStatusCode();
		gs.info(JSON.stringify(responseBody));
		if( httpStatus === 200){
			gs.info('Token: ' + responseBody);
			gs.info( 'http status: '+ httpStatus);
		}else{
			gs.info('Error Token: ' + responseBody);
			gs.error(' http status: '+httpStatus);
		}

	token = responseBody.access_token;
	gs.info(token);

@Fabrizio Joaqui The code looks good.

But can you please try the below once.

 

r.setRequestBody(body);

 

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

thanks but it doesn't works, this is my rest message:

FabrizioJoaqui_0-1671618504115.png

 

@Fabrizio Joaqui Oh the content-type is url encoded thats why its not working.

 

You need to pass the parameters in the end point like query parameters.

 

Please check this below link:

https://stackoverflow.com/questions/14551194/how-are-parameters-sent-in-an-http-post-request

 

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