How to add variables in pre request script section

How to add variables in pre request script section ?

I tired to add variables through get steps but does not worked out.

P.S. Tried with environment variables.

“username”: “{{username}}”,
“password”: “{{password}}”,

username & password already added in env variables.

Let me know if need full code

Hi @aakashjain8693, why do you need to declare the variables under pre-request section? Are you trying to add the variables from the body of the request or the previous request or something? You should be able to add the variables,

like

pm.environment.set("username", pm.variables.replaceIn('{{$randomFirstName}}'));

Trying to add data which is already I added in env variable.

Like I added username and password in env variable and now I want to access in pre request.

var username=pm.environment.set(“username”);

and now I am using {{username}} in my next steps but its getting failed.

It would be easier for people to help you solve this problem, if you added some example images of the UI that you can see in front of you :smiley:

I’m not sure where or how you have added the {{username}} syntax - It can be used in a number of different areas of the app.

If you’re trying to use this syntax in the scripts, it won’t work like that and you will either need to use pm.environment.get('username') or pm.environment.replaceIn('{{username}}') to access the value.

Agree and sorry for inconvenience.

Truly there were few confidential data but will try to provide the exact scenario.

So basically I am trying to send request from pre-request to get my token for which I am using the “send request” snippet.

Here is below looks like my request:

pm.test('post method', function()
{
pm.sendRequest({
    url: 'dsdsd/auth',
    method: 'POST',
    header: {
        "Content-Type":"application/json"
    },
    body:
    {
        mode: 'raw',
        raw: JSON.stringify(
            {
  "clientId": "",
  "clientSecret": "",
  "grantType": "password",
  "username": "fhdkjfhdkjfh",
  "password": "dhskjhdskjhd*",
  "scope": "openid email offline_access"
            })
    }
},
function(err, res){
    let response=res.json();
     pm.expect(res).to.have.property('code', 200);
    pm.environment.set("tokencoach", response.id_token);
}
)
});

So here I need to give username and password.

I tried like var username =pm.environment.get(“username”); and then in script {{username}} but no luck.

Let me know still needed any info.

You need to take this response down. It has your client id and secret in it.

Good point - removed. :trophy:

1 Like

If you were to lead with this code snippet in the original question, it would have been slightly clearer :smiley:

As that’s a string in the script you can resolve this using like this:

`${variable_name}`

More information about how that works can be found here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals


I’m not sure why your script is in the pm.test() as it’s not really testing anything and it would never fail, the sendRequest() function will work without that.

pm.sendRequest({
    url: 'dsdsd/auth',
    method: 'POST',
    header: {
        "Content-Type": "application/json"
    },
    body:
    {
        mode: 'raw',
        raw: JSON.stringify(
            {
                "clientId": "",
                "clientSecret": "",
                "grantType": "password",
                "username": `${pm.environment.get('username')}`,
                "password": `${pm.environment.get('password')}`,
                "scope": "openid email offline_access"
            })
    }
},
    function (err, res) {
        let response = res.json();
        pm.expect(res).to.have.property('code', 200);
        pm.environment.set("tokencoach", response.id_token);
    }
)