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.
Hi @bjrakshita !
You can try to achieve this with xk6-file extension.
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
xk6 build --with github.com/avitalique/xk6-file@latest
Or you can run it simply with https://github.com/szkiba/k6x after installing it.
k6x run script.js