# How to log the response of a failed requests in K6

**URL:** <https://community.grafana.com/t/how-to-log-the-response-of-a-failed-requests-in-k6/108402>\
**Category:** Grafana k6\
**Created:** [November 23, 2023, 7:55am UTC](https://community.grafana.com/t/how-to-log-the-response-of-a-failed-requests-in-k6/108402 "2023-11-23T07:55:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjrakshita](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@bjrakshita](https://community.grafana.com/u/bjrakshita)\
**Post date:** [November 23, 2023, 7:55am UTC](https://community.grafana.com/t/how-to-log-the-response-of-a-failed-requests-in-k6/108402/1 "2023-11-23T07:55:04Z")

</div>

Hello Team, Could someone please tell me how can i log the responses of failed requests to a file??

I can view them by executing console.log or by running --http-debug=full test.js \> http\_debug.txt. However, http\_debug.txt records both successful and unsuccessful measurements and I dont want to log it in consoleIn the future, there will be millions of transactions, which I believe will be unfeasible to add to http\_debug.txt or in console.

I tried checks and counters, but they only give me the amount of unsuccessful requests, not the response bodyof those requests. Is there a way to accomplish this?

I tried the suggestions in the forum, but nothing seems to work.

Thank you.

---

<div class="post-metadata">

**Author:** ![bandorko](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/bandorko/32/38733_2.png) [@bandorko](https://community.grafana.com/u/bandorko)\
**Post date:** [November 23, 2023, 11:06pm UTC](https://community.grafana.com/t/how-to-log-the-response-of-a-failed-requests-in-k6/108402/3 "2023-11-23T23:06:47Z")

</div>

Hi @bjrakshita !

You can try to achieve this with [xk6-file](https://github.com/avitalique/xk6-file) extension.

```auto
import http from "k6/http";
import file from 'k6/x/file';

const urls=["https://google.com", "https://google.com/notexists", "https://k6.io", "https://k6.io/notexists"];

function randomUrl(){
    return urls[Math.floor(Math.random() * urls.length)];
}

function headersToString(headers){
    let ret =""
    for (var key in headers){
        ret += key + ": " + headers[key]+"\n";
    }
    return ret;
}

function responseToString(r){
    let ret = "Request:\n"
    ret += r.request.method+" "+r.request.url+"\n"
    ret += headersToString(r.request.headers)+"\n";
    ret += r.request.body+"\n";
    ret += "Response:\n"
    ret += r.status_text+"\n"   
    ret += headersToString(r.headers)+"\n";
    ret += r.body+"\n";
    return ret;
}

export default function () {
  let url= randomUrl();
  let r = http.get(url);

  if (r.status != 200){ // if status is not 200 write request+response to file
    file.appendString("output.txt", responseToString(r)+'\n');
  }
}

```

You can run it with a custom k6 binary after building it with xk6

```auto
xk6 build --with github.com/avitalique/xk6-file@latest

```

Or you can run it simply with [https://github.com/szkiba/k6x](https://github.com/szkiba/k6x) after installing it.

```auto
k6x run script.js

```
