K6 load test getting 401 response

Hi, I am running a script to upload a small text file to artifactory endpoint. I am passing my API key in the header.
Below is my script:

import http from 'k6/http';
import { check, sleep } from 'k6';

  const textFile = open("/path/to/test1.txt")
  export default function () {
     const url = '<REST API end-point of artifactory>';

     const params = {
     headers: {
        Authorization: "API_TOKEN",
        'Content-Type': 'text/plain',
       },
     };
     console.log(textFile)
     const res = http.put(url, textFile, params);
     check(res, {'status was 200' : (r) => r.status == 200});
     console.log(res.status)
     console.log(res.error_code)
  }

When I am running this using k6 run I am getting below http response which is 401
INFO[0000] 401 source=console
INFO[0000] 1401 source=console

My API key is correct which I have validated using curl command.
Any idea why I am getting http 401 error. My plan is to eventually use the script in load testing artifactory REST endpoint.
Thanks

Hi @rahulsri

Welcome to the community forum :wave:

If a curl works, what can be useful is to compare the request and responses k6 is getting with http debug enabled, vs curl in verbose. There might be a difference there that helps you pinpoint the root cause, you will be able to see if k6 is actually sending the same authorization token, or if there are any other headers or request parameters that differ.

You can check the docs for --http-debug=full, and some examples in https://github.com/grafana/k6-learn/blob/main/Modules/III-k6-Intermediate/01-How-to-debug-k6-load-testing-scripts.md#use-the-http-debug-flag.

If it does not help, can you share the curl command that works for you and the (sanitized) outputs of the curl in verbose and the k6 run with http debug? I’m not sure we’ll see a lot more, though it might help to have other :eyes: on the outputs.

Cheers!

Thanks. After enabling debug mode --http-debug=full I was able to resolve the issue.

1 Like