How to get the values from response body in rest web service
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2016 02:48 AM
Hi All,
I have business rule,
function onAfter(current, previous) {
try {
var r = new sn_ws.RESTMessageV2('onelogin', 'post');
r.setStringParameter('username',current.user_name);
r.setStringParameter('lastname',current.last_name);
r.setStringParameter('email',current.email);
r.setStringParameter('firstname',current.first_name);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log(typeof(responseBody)
}
catch(ex) {
var message = ex.getMessage();
}
}
In logs i am getting the answer is string but i want an object please can one help me
Thanks
Chandu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2016 02:54 AM
What is the format of response? Print the string itself. You can understand. Is it JSON? if so, you can JSONParser.
this.json = new global.JSON().decode(responseBody);
gs.addInfoMessage(this.json.yourpropertyname);
Thanks
Rami Reddy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2016 03:18 AM
Hi Ram ,
can we add two or more than two rest messages in single business rules
function onAfter(current, previous) {
try {
var r = new sn_ws.RESTMessageV2('onelogin', 'post');
r.setStringParameter('username',current.user_name);
r.setStringParameter('lastname',current.last_name);
r.setStringParameter('email',current.email);
r.setStringParameter('firstname',current.first_name);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
//gs.log(typeof(responseBody)+"govind");
//var myObj = JSON.stringify(responseBody);
//gs.log(typeof(myObj)+"ravi");
}
catch(ex) {
var message = ex.getMessage();
}
try {
var r = new sn_ws.RESTMessageV2('onelogin', 'put');
r.setStringParameter('id', '');
r.setStringParameter('password_value', '');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.getMessage();
}
}
Can i add like this or any other way is there?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2016 03:24 AM
Yes, you can do that. But it might not be best practise. Usually we will put all this code in "Script includes" and call those functions from business rules.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2016 03:41 AM
Hi Ram reddy,
Thanks for you are reply ,
function onAfter(current, previous) {
try {
var r = new sn_ws.RESTMessageV2('onelogin', 'post');
r.setStringParameter('username',current.user_name);
r.setStringParameter('lastname',current.last_name);
r.setStringParameter('email',current.email);
r.setStringParameter('firstname',current.first_name);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log(responseBody);
//gs.log(typeof(myObj)+"ravi");
}
catch(ex) {
var message = ex.getMessage();
}
here in logs i am getting in log
{"status":{"error":false,"code":200,"type":"success","message":"Success"},"data":[{"activated_at":null,"created_at":"2016-06-27T10:36:58.860Z","email":"dfd@gmail.com","username":"bbvb","firstname":"cvvbvxc","group_id":null,"id":23461532,"invalid_login_attempts":null,"invitation_sent_at":null,"last_login":null,"lastname":"cvb","locked_until":null,"notes":null,"openid_name":"dfd","locale_code":null,"password_changed_at":null,"phone":null,"status":null,"updated_at":"2016-06-27T10:36:58.899Z","distinguished_name":null,"external_id":null,"directory_id":null,"member_of":null,"samaccountname":null,"userprincipalname":null,"manager_ad_id":null,"role_id":null}]}
here i need the id value in variable
var r = new sn_ws.RESTMessageV2('onelogin', 'put');
r.setStringParameter('id',);
r.setStringParameter('password_value', '');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
i have to pass the id value but how can i do it.