Cannot parse json

hi all. it seems im running into this error when trying to run my k6 file

cannot parse json due to an error at line 1, character 2 , error: invalid character '<' looking for beginning of value
running at reflect.methodValueCall (native)
default at is authenticated (file:///Users/halshere/Desktop/testing_ground/ksix/auth.js:20:31(2))
        at go.k6.io/k6/js/modules/k6.(*K6).Check-fm (native)
        at file:///Users/halshere/Desktop/testing_ground/ksix/auth.js:18:13(35)
        at native  executor=per-vu-iterations scenario=default source=stacktrace

and im not too sure how to fix this issue

Hi @halshere, Welcome to the community forum :tada:

It will be hard to say what exactly happened but I would expect you tried to parse an http response as json, but in this case it wasn’t a json and you got this error.

This sometimes happen under load when the server starts returning error page instead of a json. So in that case the http response is <html>..... and this is basically what the errors tell you - it did not expect < it did expect JSON.

You should be doing some prelimanary checks that your responses are at least somewhat what you expect before trying to parse them in specific ways.

The guide on running large test has a small section on making your test resilient which gives more detail. But arguably in this case you should only try to parse it as json in (for example) the status code is 200 or w/e you are expecting.

Hope this helps you!

hi @mstoykov thank you for responding. Here is my script im trying to get working. I want to use k6 to load test a staging instance I have running and even when given proper params, it gives me the error I posed in my question

import encoding from 'k6/encoding';
import http from 'k6/http';
import { check } from 'k6';

const username = 'user';
const password = 'pass';

export default function () {
  const credentials = `${username}:${password}`;

  // Passing username and password as part of the URL will
  // allow us to authenticate using HTTP Basic Auth.
  const url = `http://${credentials}@staging29.drizly.com/${username}/${password}`;

  let res = http.get(url);

  // Verify response
  check(res, {
    'status is 200': (r) => r.status === 200,
    'is authenticated': (r) => r.json().authenticated === true,
    'is correct user': (r) => r.json().user === username,
  });

  // Alternatively you can create the header yourself to authenticate
  // using HTTP Basic Auth
  const encodedCredentials = encoding.b64encode(credentials);
  const options = {
    headers: {
      Authorization: `Basic ${encodedCredentials}`
    },
  };

  res = http.get(`https://staging29.drizly.com/${username}/${password}`, options);

  // Verify response (checking the echoed data from the httpbin.test.k6.io
  // basic auth test API endpoint)
  check(res, {
    'status is 200': (r) => r.status === 200,
    'is authenticated': (r) => r.json().authenticated === true,
    'is correct user': (r) => r.json().user === username,
  });

  console.log(res.json())
}

check does not stop the test or the rest of the checks in case that any of it’s check fail.

So in this case I would expect the first check to fail, but it will still try to parse the response as JSON and that throws an exception - hence the error you see.

You can try to try/catch the exception and console.log(r, null, " ") will print out the whole response “nicely”.

But in case the status code is not 200 you likely just got an error.