cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

get body object from previous request HTTP monitoring

nitinkumar
Newcomer

How to get body from previous request to new request in body section only as raw

 

1st request setting

api.setValue("req_body", jsonData);

 

2nd request getting 

In pre execution-

var requestBody =api.getValue("req_body");
var replace_this_body_data=JSON.stringify(requestBody);
api.setValue("replace_this_body", JSON.stringify(requestBody))
api.info("JSON data from 1st request");
api.info(requestBody);

 

in body

{replace_this_body_data}

 

I am not getting anything in replace_this_body_data in body section

7 REPLIES 7

HannahM
Dynatrace Champion
Dynatrace Champion

What is setting jsonData in your first request? Are you using response.getResponseBody()?

yeah, its something like that in previous request

var responseBody = response.getResponseBody();
var jsonData = JSON.parse(responseBody);

 

I modified the data in jsonData and passed it in first request like.

api.setValue("req_body", jsonData);

 

 

Now in second request in pre execution I wrote the code shared above in description.

 

 

AND IN BODY raw i am trying to access it.

I am getting  "requestBody": "[object Object]", It should be some JSON object(string) so it can be sent in next request again.

 

HannahM
Dynatrace Champion
Dynatrace Champion

What happens if you don't use stringify? I believe it's a string until you start to parse it etc anyway so I don't believe this is needed. 

Hi HannahM, I tried without that also, Its returning same object Object in requestBody": "[object Object]

HannahM
Dynatrace Champion
Dynatrace Champion

Hi Nitin, 

I see you already have a ticket open with our Scripting team for this. I will leave it for them to assist you further. 

Best wishes, Hannah

yeah, thanks

HannahM
Dynatrace Champion
Dynatrace Champion

Solution: Requests and their responses can only be interrogated within that request. So if you would like to pull a value from a request's response. It must be done in the post-execution script of that request as the next request will not have access to it.

Solution for the OP was to add the following to the post-execution script of the 1st request: 

if (response.getStatusCode() != 200) {
api.fail("HTTP error: " + response.getStatusCode());
}

var responseBody = response.getResponseBody();
var jsonData = JSON.parse(responseBody);
api.setValue("authIdToken", jsonData.authId);
api.info(jsonData.access_token);