4

As the title implies, as of now I'm facing some "doubts" regarding the Authorization with Bearer Token using K6 given that I'm receiving that Token as access_token from a getAuth method/service, right now I have the following:

getAuthtoken.js

import http from 'k6/http';
import { check, sleep } from "k6";
import * as general_data from '../utility/general.js';
import * as auth_data from '../utility/authentication.js';

export default function () {
  var url = `${general_data.baseUrl}${auth_data.url}`;
  var payload = JSON.stringify(auth_data.body);

  const headers = {
    headers: auth_data.headers,
};
  const res = http.post(url, payload, headers);
  check(res, {
      'status was 200': r => r.status == 200,
  })
  console.log(res.body)
  return res.json()
}

The Json response looks like this:

{
    "access_token": "vHS9awrfrzTWxMGpAsVNWD2aSsKXkoGBZg8RgCSoQDo",
    "token_type": "Bearer",
    "expires_in": 900,
    "refresh_token": "f2SxXA0Nbr-OXE8RlVonej3xaKc-zifO4ZVMcGIGTAM",
    "scope": "public",
    "created_at": 1643633573
}

So I need to somehow grab this access_token and the token type bearer in order to be able to use it in another .js file and consume each and every API method, for example, use it in this script:

getWorkflowsRampUp.js

import http from 'k6/http';
import { check, sleep } from "k6";
import * as general_data from '../utility/general.js';
import * as auth_data from '../utility/authentication.js';
import * as workflows_data from '../utility/workflows.js';

export let options = {
    stages: [
        { duration: "10s", target: 2 }, 
        { duration: "20s", target: 5 },      
        { duration: "5s", target: 0 }
    ]
  };

export default function () {
  var url = `${general_data.baseUrl}${workflows_data.url}`
  var payload = JSON.stringify(auth_data.body);
  const res = http.get(url, payload, {
    headers: auth_data.headers
    }
  );
  check(res, {
      'status was 200': r => r.status == 200
  })
  console.log(res.body)
  sleep(1);

}

Any guidance would be greatly appreciated since I haven't been able to find any specific information or guide in doing these even bought some Udemy courses and didn't even come close to this scenario :(

Thanks in advance as always guys and have a nice week!

Esepee
  • 87
  • 1
  • 7
  • 1
    Where do you call the function of file `getAuthtoken.js` and what's the content of `auth_data.headers`? Have you tried running with http debug enabled so that you can inspect the actual HTTP requests being sent? – knittl Feb 01 '22 at 19:18
  • Thanks for your answer @knittl finally got it solved my error was due to me passing the Payload unnecessarily (facepalm hahaha) Nevertheless I have never used http debug for Javascript neither know how to enable it, any chance you could point me out how to actually do it! Thanks once again for your reponse! – Esepee Feb 01 '22 at 19:21
  • 1
    [HTTP debugging](https://k6.io/docs/using-k6/http-debugging/) is a functionality provided by k6, not JavaScript. It can be enabled with `--http-debug=full` – knittl Feb 02 '22 at 19:40
  • 1
    Voting to close, because the error was a typo (in the widest sense) and this is not reproducible – knittl Feb 02 '22 at 19:41

0 Answers0