OIDC openid connect token auth API in K6

I just started my journey with Javascript and also with K6.
Is there anyone here that would like to me to write a code that would generate an authorization ‘Bearer token’?

Below is a photo of an example my token generator in postman.

Here is my current code, but it is not effective, as I have to copy and paste the token from postman generator to my K6 script:

export default function(){
    let params = {
        headers: {
          'Content-Type': 'application/json',
          Authorization: `Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJ2dTFmbFlpZkNpSzEwUEJyU0tUZzU5TXR4TUJvMC1KZXR4........`, 
  let res = http.get('https://api.example.com/customers/11177/account/balance', params);
  check(res, { 
      'status of account balance was 200': (r) => r.status == 200, 

Hey @fatkobra ,

You can use the k6 setup functions. You can call the value you get from the setup in the next functions.

For example;

import http from "k6/http";

export function auth_user() {
    const res = http.get('https://jsonplaceholder.typicode.com/users')
    return res.json()[0].name;

export function setup() {
    const access_token = auth_user();
    return  access_token;

export default function (access_token) {

Thank you @yusuftayman

Still to advance for me.

I might have a question or 2 later.

1 Like