Is there a way to store a newly-created user's password in a variable, when the password is not displayed in the response body?

Is there a way to store a newly-created password in a variable, especially if that password is not returned as part of the response body?

I am quite new to working with variables in Postman. Am currently working with a test API where I create a new user (having “name”, “email”, and “password” as the request body), and then after I log in with the credentials I have created above (that is, “email” and “password”).

Under the pre-request script, I create a random name, a random email and a random password and assign these to respective variables like so:
image

So this is what my request body looks like:
image

And this is what my response body looks like:
image

Now, I want to get the random password that is created (but not displayed in the response body) and store it in a variable such that I can pick it up later and use it while logging in the same newly-created user.

When I attempt to log in with the newly-created random email and password like this:

{
    "email":"{{email}}",
    "password":"{{password}}"
}

, I get a Bad Request error code (400) and my response body looks like this:

{
    "Message": "The request is invalid.",
    "ModelState": {
        "log.password": [
            "An error has occurred.",
            "An error has occurred."
        ]
    }
}

yet I intend to log in as the new user (Response code should be 200 and the user details should be displayed).

I have tried to look around online for such a solution but I seem not to find an appropriate answer. Any help is highly welcome and greatly appreciated.

Hi @katamba

Your password is stored in a variable;
image

Can you see the password being passed in the request from the console log?

I was working with someone on here last night who had a 400 error and it was because the web browser was holding some data. If you are using a browser version, try clearing the cache, or use incognito mode… or try the desktop client.

Thanks a lot for your response!

Your password is stored in a variable;
image
Can you see the password being passed in the request from the console log?

Yes, I get to see the password generated in the console when I log it, but when I use it to log in the user, it doesn’t work, and Postman displays that error as I mentioned earlier. Am not sure why :frowning: . Also, I am using the Desktop client, not the browser version.

Could you share the output from the console logs?
And possibly a screenshot of the call you make to log a user in?


In the “log in” call, if you add a

console.log(pm.environments.get("password"));`

to the pre-req or test tab… does it output the password in the console log?


You could also check the host is being sent properly, this value can be hardcoded in the header if needed;
image

So, when I add this to either the pre-req or test tab and run the script, this is the error message I get:

This is how I’ve been logging the password to the console:

The call am making to log a user in, looks like below:

{
	"email":"{{email}}",
       "password": 1234567
}

The password here is hardcoded because the randomly created password failed to work like I mentioned earlier before.

And do you see this value in your environment file?
image

Like this;
image
But with your expected password value?

Yes sir, I do see the value:

So one thing I just noticed,

In this screenshot you are not setting your environment variable (you can’t “get” something that isn’t “set”

I think you would need to add a pm.environments.set() to your pre-req (between lines 1 and 2).

I haven’t had time to read through the other stuff yet but let me know if that changes anything.

In your JSON body, your password is not a number, it contains both letters and characters, so I think it needs quotes…

If you add “ ” to your password it passes with a 200 response code.

1 Like

Thank you very much, @w4dd325 ! This resolves my issue :partying_face:!