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

Shekhar Navhak1
Kilo Sage
Kilo Sage

Hi Fabrizio,

 

The above parameter is not to send from the body, it should be before sending the payload.

 

Please see the below example

var oAuthClient = new sn_auth.GlideOAuthClient();
var params = {grant_type:"client_credentials", client_id:'xxxx', client_secret:'xxxx'};
var json = new global.JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken('xxx', text);
var token = tokenResponse.getToken();
gs.log("AccessToken:" + token.getAccessToken());
gs.log("AccessTokenExpiresIn:" + token.getExpiresIn());
gs.log(" RefreshToken:" + token.getRefreshToken());

 Then your actual body.

 

Please mark helpful OR correct as per the impact.

Please mark the answer correct/helpful based on Impact.
Regards, Shekhar

thanks, but how can i do in rest message?

FabrizioJoaqui_0-1671618640710.png

this is my code:

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

Hi @Fabrizio Joaqui ,

 

Please refer below code:

 

 

var r = new sn_ws.RESTMessageV2('RESTMessage', 'post');
//Auth2.0 header
r.setRequestHeader("Content-Type","application/json");
r.setRequestHeader("Bearer Token"+accessToken);
r.setAuthentication("oauth2", "xxx default_profile");

//request body of payload 
r.setStringParameter("number",current.number);
r.setStringParameter("description",current.description);
r.setStringParameter("priority",current.priority);

//Response 
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

 

Please mark the answer correct/helpful based on Impact.
Regards, Shekhar

Dhruv Chandan
Giga Guru

Hi @Fabrizio Joaqui ,

 

Could you try the below code :-

 

var req = new sn_ws.RESTMessageV2();
req.setEndpoint('https://--- your end-point here');
req.setHttpMethod('<Transaction_Type');

//Authentication
var username = user; // user_name value
var pass = password; // password value
req.setBasicAuth(user,pass);

//Set Headers
req.setRequestHeader('client_id','SNIT0da4-b027-4a81-a4e7-c6352a315759');
req.setRequestHeader('client_secret','3K72Yv6F7_zLS7zZZc4LW3wHsG4s9Vc1XO7rSfkM');
req.setRequestHeader('grant_type','client_credentials');
req.execute();

 

Thanks,
Dhruv