Getting 401 Unauthorized + debugger inconsistencies + other errors

Hello everyone, currently I am developing a new load testing framework with k6 and webpack bundle to build it up and I am facing a couple of errors, I don’t know what I am doing wrong hope you can help me!

As you can see in the image code is very simple

Have 2 groups >> One that uses a ‘Common’ class to call and endpoint, only one time, to get the authorization Token, and the other group that contains the testing http.get methods

1. Use of session
As you can see I am using session to do all the http calls. This session variable is created in ‘Common’ class to setBaseUrl properly one time. The issue is that when I use default http.get or http.post I get this error, that forces me to use session.
TypeError cannot read property post of undefined or null

2. 401 Unauthorized
So first endpoint works perfect, I get the auth Token right but when I do the first get request I get 401 unauthorized, which makes no sense because I put same request on thunder client/postman and I get all the info with 200 OK.
As i cannot get more info I decided to debug >>

MP13-M-04243:load-performance nil.pascual$ k6 run --http-debug scripts/users.js

          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: scripts/users.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

INFO[0001] Request:
POST /v1/user/login HTTP/1.1
Host: api.staging.mydomain.com
User-Agent: k6/0.39.0 (https://k6.io/)
Content-Length: 266
Content-Type: application/json
Accept-Encoding: gzip

  group="::Authenticate the client..." iter=0 request_id=c3706842-8f22-4656-6aa1-cc6bfe5e8f2c scenario=default source=http-debug vu=1
INFO[0006] Response:
HTTP/2.0 200 OK
Content-Length: 2953
Access-Control-Allow-Headers: <redacted>
Access-Control-Allow-Origin: *
Cache-Control: no-store, private
Cf-Cache-Status: DYNAMIC
Cf-Ray: 74059858a8f788c1-LHR
Content-Type: application/json
Date: Thu, 25 Aug 2022 16:10:02 GMT
Expect-Ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Frame-Options: SAMEORIGIN
X-Ratelimit-Limit: 100
X-Ratelimit-Remaining: 99

  group="::Authenticate the client..." iter=0 request_id=c3706842-8f22-4656-6aa1-cc6bfe5e8f2c scenario=default source=http-debug vu=1
INFO[0006] User authToken is >> redacted  source=console
INFO[0011] authToken Testing >> redacted && params >> {"headers":{"Content-Type":"application/json","access-token":"redacted"}}  source=console
INFO[0015] Request:
GET /v1/user/profile HTTP/1.1
Host: api.staging.mydomain.com
User-Agent: k6/0.39.0 (https://k6.io/)
Content-Length: 102
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip

  group="::Execute Users Group tests..." iter=0 request_id=42eca067-5ce1-4ec2-7c8e-0dd16ce572ca scenario=default source=http-debug vu=1
INFO[0015] Response:
HTTP/2.0 401 Unauthorized
Content-Length: 91
Access-Control-Allow-Headers: <redacted>
Access-Control-Allow-Origin: *
Cache-Control: no-store, private
Cf-Cache-Status: DYNAMIC
Cf-Ray: 740598b1ce3288c1-LHR
Content-Type: application/json
Date: Thu, 25 Aug 2022 16:10:12 GMT
Expect-Ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
X-Frame-Options: SAMEORIGIN

  group="::Execute Users Group tests..." iter=0 request_id=42eca067-5ce1-4ec2-7c8e-0dd16ce572ca scenario=default source=http-debug vu=1
INFO[0015] Response status = 401                         source=console
INFO[0020] Request:
GET /v1/user/settings HTTP/1.1
Host: api.staging.mydomain.com
User-Agent: k6/0.39.0 (https://k6.io/)
Content-Length: 102
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip

  group="::Execute Users Group tests..." iter=0 request_id=8c79a01d-eab0-48b4-4f8c-f73411af7832 scenario=default source=http-debug vu=1
INFO[0021] Response:
HTTP/2.0 401 Unauthorized
Content-Length: 91
Access-Control-Allow-Headers: <redacted>
Access-Control-Allow-Origin: *
Cache-Control: no-store, private
Cf-Cache-Status: DYNAMIC
Cf-Ray: 740598d1ea5f88c1-LHR
Content-Type: application/json
Date: Thu, 25 Aug 2022 16:10:17 GMT
Expect-Ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
X-Frame-Options: SAMEORIGIN

  group="::Execute Users Group tests..." iter=0 request_id=8c79a01d-eab0-48b4-4f8c-f73411af7832 scenario=default source=http-debug vu=1```
 here we can see a couple of things: the groups doesn't coincide exactly with my code, and the request that is doing contains "Content-Type": application/x-www-form-urlencoded when I am explicitly sending 'application/json'

To finish I share also the common class, where if I change this session.post() with http.post (Importing http from 'k6/http') I get same TypeError cannot read property post of undefined or null 
![Captura de Pantalla 2022-08-25 a las 18.38.23|394x500](upload://d31xF9vAaIx7NE9nFtAVc6VBvrQ.png)

Thanks everyone! Hope everything is clear.

Hi @NilPascual !

1. Use of session

That’s interesting. Could you please adjust the original post since the formatting hides the image?

TypeError cannot read property post of undefined or null

Could you please provide a script file that errors with such a message?

2. 401 Unauthorized

In the first GET request, I see neither Content-type nor access-token. This probably means that something meaningful can be in the common.getUserSession, so, please attach it there :relaxed:

Cheers!

Hello, thanks for the answer, the issue was that when you use that session() thing with httpx, get() method changes to get(url, body, params) which makes no sense 'cause, if I am not wrong, get methods don’t send any body, but I do now the same call this way session.get(url, null, params) and works.

1 Like