How to set a variable in pre-request to use it in following pre-request of same request

I’m trying to send a delete request for an article, but firstly it is needed in pre-requests to register a new user (1) and then create the article (2).
I need to use a token obtained in registration to create an aritcle, but in this pre-request incorrect token is used (which was previously stored).
So, I’d like to ask, how to use correct token in the 2nd pre-request that is obtained from 1st pre-request.
Many thanks.

Collection link

(1) - is written in collection folder in Pre-Req Scripts as variable:

postman.setEnvironmentVariable('RegPreReq', () => {
    const url = "https://conduit.mate.academy/api/users";
    const username = pm.variables.replaceIn("{{$randomWord}}_{{$randomWord}}00");   
    const email = pm.variables.replaceIn("{{$randomWord}}_{{$randomWord}}00@gmail.com");


    const registrationRequest = {
        url: url,
        method: 'POST',
        header: {
            'Content-Type': 'application/json',
        },
        body: {
            mode: 'raw',
            raw: JSON.stringify({
                "user": {
                    "username": username,
                    "email": email, 
                    "password": "!Conduit#"
                }
            })
        }
    };
    pm.sendRequest(registrationRequest, (error,response) => {
        console.log(error ? error : response.json());
        pm.environment.set("TOKEN_NEW", response.json().user.token);
        pm.environment.set("LOGIN_EMAIL", response.json().user.email);
    });

(2) is written as variable in the request folder in Pre-Req Scripts

postman.setEnvironmentVariable('ArticlePreReq', () => {
    const randomWords = pm.variables.replaceIn("{{$randomWord}} {{$randomWord}} {{$randomWord}}");
    
    const CreateArticleRequest = {
        url: 'https://conduit.mate.academy/api/articles',
        method: 'POST',
        header: {
            'Content-Type': 'application/json',
            'Authorization': 'Token '+ environment.TOKEN_NEW,
        },
        body: {
            mode: 'raw',
            raw: JSON.stringify({
                    "article": {
                    "title": randomWords,
                    "description": randomWords,
                    "body": randomWords,
                    "tagList": [
                         "Test_tag"
                         ]
                     }
            })
        }
    };
    pm.sendRequest(CreateArticleRequest, (error,response) => {
        console.log(error ? error : response.json());
        pm.environment.set("SLUG", response.json().article.slug);
    });
});

And in the end i make eval in the pre-req of DELETE request:
eval(environment.RegPreReq)();
eval(environment.ArticlePreReq)();

The first part of the token looks ok and at first thought I thought it was being cropped. Can you confirm if you are looking at the collection variables or the environment variables. It looks like you are looking at the collection variables, but your code is setting an environment level variable. Is it possible that the variable is duplicated at the environment level.

Using variables | Postman Learning Center

However, why do you need pre-requests for this?
Is this just how the training is telling you to do this?

If you’ve got a pre-req script for your registrationRequest at the Collection level then it will execute before every request in the collection (and update the token each time). Is this correct behaviour? When you run the create request, it will run the registration and create a token. When you run the delete request, it will run the registration again, and create a new token (and overwrite the variable). What I can’t tell is if you are running these tests as a collection from the runner or not in which cause running the registration code for each request doesn’t seem right.

It looks like you just need to ensure your collection runs in the correct order.

Registration → Login → Create Article → Delete Article

Use the Tests tab for the Registration request\response and set a collection variable for TOKEN_NEW so its available for all subsequent requests in the collection.

pm.collectionVariables.set('TOKEN_NEW', response.json().user.token);

You can call that token in any subsequent request using {{TOKEN_NEW}}

1 Like