Pm.response.body logging as undefined

Hi!

Im pretty new to Postman and im trying to figure out how to set an env variable with the response of an api call im doing.

The API returns Status 200 OK and returns this body:

“EXT<Tksmau6zx0bvA3AoR0;count=28;>”

i understand i can do this via creating a “test” like so:

var response = pm.response.body;
pm.environment.set(“variable”, response);

this keeps setting the variable as null therefore i tried using the console log to debug what was going on :

console.log("This is a test "+pm.response.body);

and it is printing:

“This is a test undefined”

I am unsure what I may be doing wrong and searching online hasn’t yielded much help there for any advice would be really appreciated

Thanks!

Hey @robertovc93

It might be would taking a look through our learning site to get an idea about what you can do with the pm.* API.

https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/

Hi!
Thanks for the help

I already checked that and that’s how I attempted to do:

console.log("This is a test "+pm.response.text);

however it is now just outputing this instead of the body that i mentioned above:

“This is a test function(){return this.stream?util.bufferOrArrayBufferToString(this.stream,this.mime().charset):this.body}”

You were very close :slight_smile:

This is what you would need to capture the full response body as text pm.response.text(), it’s a function so you need to add the additional () to the end.

Your log statement should look something like this:

console.log("This is a test "+ pm.response.text());

or

console.log(`This is a test ${pm.response.text()}`);

Was there a specific part of the “EXT<Tksmau6zx0bvA3AoR0;count=28;>” response that you needed to capture in a variable?

Thanks!! it worked! i dont know how i missed the “()”

and to answer your question, yeah, im gonna trim the " at the beginning and at the end before assigning to the variable

1 Like