Hi,
I found that k6 always sends the http headers’ value as an array of string. For example, if I send something like this:
http.get(`${url}/session`, {
'Content-Type': 'application/json',
'Connection': "keep-alive"
});
The server gets:
{
"method": "GET",
"url": "https://somewhere",
"headers": {
"User-Agent": [
"k6/0.37.0 (https://k6.io/)"
],
"Content-Type": [
"application/json"
],
"Connection": [
"keep-alive"
],
},
}
Is it possible to make k6 send the values as simple string?
Tom_1
March 29, 2022, 2:20am
2
Hello!
That code should not result in those headers being set - the key=value pairs need to reside within the headers
object like this:
http.get(`${url}/session`, {
headers: {
'Content-Type': 'application/json',
'Connection': "keep-alive"
}
});
You probably just accidentally left it out of the code snippet though
That JSON is actually what you’d see if you JSON.stringify
the request headers. Here’s an example:
import http from 'k6/http';
export default function () {
let res = http.get('https://test-api.k6.io/public/crocodiles/', {
headers: {
'Content-Type': 'application/json',
'Connection': 'keep-alive',
}
});
console.log(JSON.stringify(res.request.headers, null, 2));
}
Yields:
{
"User-Agent": [
"k6/0.37.0 (https://k6.io/)"
],
"Content-Type": [
"application/json"
],
"Connection": [
"keep-alive"
]
}
This is not actually what the server ends up seeing - those arrays do get flattened at some point (I think here , but I might need our devs to confirm that ).
As a general HTTP debugging tip, using a web proxy might yield the most insight into what the server actually ends up seeing - they are great as they are in effect a 3rd party to the HTTP conversation so you know exactly what’s going across the wire and back.