The CreatorCon Call for Content is officially open! Get started here.

How to parse response body?

tyagisu
Mega Expert

Hi,

My requirement is to get the message and type from the response body. When I get status code 500, this is the response body I have. I am using the script include for calling the rest API and then calling this API in one of the UI actions. So I need to show this message and type I get in my response body when I click the UI action.

ResponseBody:

{
"error" : {
"type" : "SERVICE_EXCEPTION",
"message" : "MTA may be in use and determined to be not safe to suspend."
}
}

Any help would be appreciated!

Thanks,

Su

1 ACCEPTED SOLUTION

Cool. Can you now replace 

type = res.type;

message = res.message;

with 

type = res.error.type;

message = res.error.message;
Regards,
Muhammad

View solution in original post

17 REPLIES 17

Thanks,

I need to get the value of message and type, after parsing the response body.

Thanks,

Su

Hi,

If you need  type and message,then you can follow the below script,

After the above code mentioned:-

if(response){

var type = response.type;

var message = response.message;

gs.addInfoMessage("Response Type: "+type+ " And Message is: "+message);

}

 

If i was able to solve your query, please mark my answer correct and helpful.

Thanks & Regards

Prasant kumar sahu

tyagisu
Mega Expert

It didn't work either.

var response = suspendRequest.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var res = JSON.parse(responseBody);

if(res){
var type = res.type;
var message = res.message;

gs.info("Response Type: "+type+ " And Message is: "+message);

}

Also, tried this one, didn't work either...

if(response ){
var type = response .type;
var message = response .message;

gs.info("Response Type: "+type+ " And Message is: "+message);

}

 

Thanks