06 Dec 2021 09:18 AM
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
Solved! Go to Solution.
06 Dec 2021 09:56 AM
What is setting jsonData in your first request? Are you using response.getResponseBody()?
06 Dec 2021 10:08 AM
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.
06 Dec 2021 10:28 AM
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.
06 Dec 2021 10:42 AM
Hi HannahM, I tried without that also, Its returning same object Object in requestBody": "[object Object]
06 Dec 2021 10:48 AM
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
12 Jul 2022 10:28 AM
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);