Postman pre-request script does not set the variable for an access token

I am trying to make a pre-request script for request A that executes another request B for a login function and generating an access token that is saved to a variable but the problem is that for some reason the pre-request script is not executed, the access token remains null and request A will fail due to invalid token error 401.

Request B has basic Authorization in the headers

If I run request B individually it is run successfully and the variable I set gets updated correctly.

This is the script I am using in request A to execute request B

pm.sendRequest("url.example/post", function (err, response) { console.log(response.json()); });

What could be wrong ? Is there any other info i need to provide ?

Hey there! First, I’d like to welcome you to the Postman community and congrats on your first post! :confetti_ball:

So the issue that you are most likely having here is that pm.sendRequest is an asynchronous function. This means that the code runs but is not completed before going on to the next line in your pre-request script. In other words, you are making the post request and that code is executing, however it is happening after the rest of your code that depends on a successful auth.

Here is one solution that I’ve used in the past to acomplish what you’re trying to do. Hope it helps! :smile: