Dear Grafana Community,
I am quite a rookie to k6, and I’m encountering an issue with my test script while trying to make a PUT request to a website. Interestingly, the request works perfectly fine in Postman, returning a 200 status code, indicating success.
However, when I try to replicate the same request using k6, I consistently receive a 400 Bad Request error.
I’ve double-checked the script and made sure that all parameters and headers are correctly set, but I’m still unable to pinpoint the source of the problem. Here is the k6 script:
import http from 'k6/http';
import { check, group, sleep } from 'k6';
import { parseHTML } from 'k6/html';
import { randomString } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
let csrfToken;
export let options = {
insecureSkipTLSVerify: true,
noConnectionReuse: false,
vus: 1,
duration: '5s',
};
export default function () {
group('Login', function(){
// --- Get CSRF Token ---
let loginPageResponse = http.get('https://login);
// Parse HTML content using k6's html module
let parsedHTML = parseHTML(loginPageResponse.body);
// Extract CSRF token directly from the HTML response using k6's HTML parsing
csrfToken = parsedHTML.find('[name="__RequestVerificationToken"]').attr('value');
// --- Login ---
let loginResponse = http.post(
'https://login',
{
UserName: '****',
Password: '****',
__RequestVerificationToken: csrfToken,
},
{
headers: {
"Content-Type": 'application/x-www-form-urlencoded'
}
}
);
check(loginResponse, {
'login ok': (r) => r.status === 200,
});
sleep(1);
});
group('Create', function(){
let vocab = http.post(https://something/Create',
{
Original: 'test_' + randomString(3),
Corrected: 'test_' + randomString(3),
__RequestVerificationToken: csrfToken,
},
{
headers: {
"Content-Type": 'application/x-www-form-urlencoded'
},
}
);
console.log('Response:', JSON.stringify(vocab));
console.log('Response Body:', vocab.body)
check(vocab, {
'Vocabulary created successfully': (r) => r.status === 200,
});
});
}
Has anyone else experienced a similar issue with k6 or have any suggestions on how to troubleshoot and resolve this discrepancy between Postman and k6?
Any insights or assistance would be greatly appreciated!
Thank you."