How to use the authorization token in a variable which can global, and can be used to another request header

Hi, I am trying to save the authorization token from login request and want to set the same variable in another request’s header. I am new to k6 and need some regarding the same. My code is as below:

response = http.post( commonUrl + '/login-web-v3', login, {headers: headers});
check (response, {'status is 200': r => r.status == 200});

auth = console.log('Authorization token is: ' + String(response.json("oBody.payLoad.sAuthorizationToken")));
console.log('Authorization token stored is: ' + auth);

/response = http.post( commonUrl + '/master/search/get-drop-down-master', fos + '/Sourcing/dropdown.json', {headers: fos_headers});
 check (response, {'status is 200': r => r.status == 200});

Hi @dee.adya1
the line

auth = console.log('Authorization token is: ' + String(response.json("oBody.payLoad.sAuthorizationToken")));

likely should be

auth = String(response.json("oBody.payLoad.sAuthorizationToken"));

And then you just set it in fos_headers as in

fos_headers["Here-is-the-correct-header"] = auth;

Hope this helps and good luck

Thanks it helped. Just one question, if I have multiple requests in same function then is the excution sequential? because until I receive the response of 1st request cannot pass the response variable in the second request. the auth needs to be passed in header and print the value passed?

var fos_headers = {
                    Host: "ssgpreprod.serviceurl.in",
                    Connection: "keep-alive",
                    Accept: "application/json, text/plain, */*",
                    "Content-Security-Policy": "default-src 'self'",
                    Authorization: "Bearer ${auth}",
                    institutionID: "4032",
                    code: "",
                    "User-Agent": "",
};

yes, the execution is sequential

how can I print the headers to check the value? As i need to verify {auth} value is getting stored correctly. I tried this but it print to headers as below but it gives object object error alsoif I print value of {auth} it does not print the value
console.log('sendmail stored is: ’ + fos_headers);

Similiarly not able to pass the ${auth} value in requestData.json