Hello, I’m trying to get data from my post request, specifically either from request or response body.
Below is what I’m using to get data from the responseHeader.
pm.test("Create Account", function() {
let headerValue = pm.response.headers.get("Neededdata");
pm.globals.set("Neededdata", headerValue);
});
However, I cannot get it to work when I’m trying to extract data from response/request body, how can I call on the body?
Hey @theToncheff
To get the Request Body
data you can use the pm.request
object and extract the details from there.
For example, this would set the raw Request Body
as a Global
variable:
pm.globals.set("requestBody", pm.request.body.raw)
Likewise, for the Response Body
you can use the pm.response
object to extract the data.
For example, this would set the raw Response Body
as a Global
variable:
pm.globals.set("responseBody", pm.response.json())
More information about the pm.*
API functions can be found here:
https://learning.getpostman.com/docs/postman/scripts/postman-sandbox-api-reference
Getting specific values from either of those would depend on the structure of the data, I don’t know what that is so this isn’t a complete solution - Just a helping hand
1 Like
If your response is in JSON format I recommend:
var jsonData = JSON.parse(responseBody);
pm.environment.set(“variable name”, jsonData.path.to.value);
This is not working now, The old one ( JSON.parse(responseBody); ) deprecated. Could you provide variable for below syntax
pm.response.text()
Hey @altimetry-operator-1
As you’re responding to a comment that’s 5 years old - there might be some context that you need to provide here.
Can you fully expand on the details of your issue and the response you have, in order for people to assist you.