Log Request URL and Body for failing requests

Hi,

In our tests observing errors for some requests mainly POST request. We have parameterized and correlated server dynamic values in Post body. Would like to log request Body and URL for failing request.

How this could be implemented?

Thanks

Hello!

Here’s a basic example, assuming that the server errors with HTTP 500 (which may not be the case for you):

import http from "k6/http";

export default function() {
  const response = http.post("https://httpbin.org/status/500", { foo: "bar" });
  if (response.status === 500) { // adjust as necessary
    console.error(`Received HTTP 500 for ${response.url}, body: ${JSON.stringify(response.request.body)}`);
  }
}

The key thing being that the response contains a reference to the request it originated from. The above is probably OK for debugging the script, but if you are finding that this happens only under load, you may want to use a check so that you can then ascertain the frequency (%) of errors more easily.

1 Like