How to run one request from another using Pre-request Script in Postman

后端 未结 6 1251

I\'m trying to send an authenticated request with one click in postman.

So, I have request named \"Oauth\" and I\'m using Tests to store the token in a local variable.

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 02:35

    You can add a pre-request script to the collection which will execute prior to each Postman request. For example, I use the following to return an access token from Apigee

    const echoPostRequest = {
      url: client_credentials_url,
      method: 'POST',
      header: 
          'Authorization: Basic *Basic Authentication string*'
    
    };
    
    var getToken = true;
    
    if (!pm.environment.get('token'))
    
    {
        console.log('Token  missing')
    
    }
    else 
    {
    
        console.log('Token all good');
    }
    
    if (getToken === true) {
        pm.sendRequest(echoPostRequest, function (err, res) {
        console.log(err ? err : res.json());
            if (err === null) {
                console.log('Saving the token');
                console.log(res);
                var responseJson = res.json();
                console.log(responseJson.access_token);
                pm.environment.set('token', responseJson.access_token)
    
    
            }
        });
    }
    

提交回复
热议问题