How to set empty header in K6

Hi,

for one of the custom headers, I need to set it to an empty value. Somehow I am not able to.
I tried setting it to “”, " ", ‘’, ’ ',``, , null, undefined but still I don’t think it’s working.

To give a reference in RobotFrameworks, there is a defined value called {EMPTY}, when I set header to this empty value it works through robot frameworks.

Is there something similar in JavaScript?

This is how I am setting my header :

const custom_header = {

headers: {

'Authorization': __ENV.Token,  

'Content-Type' : 'application/json',

'bID' : ""

}

}
if I set it to bID: undefined or bID : null then also it shows that its set as bID: [“null”] or biD: [“undefined”]. How do I set it as bID: []

please suggest.

Hi @Jay

Let me try to help you!

I believe passing the empty string should work. To prove that, you can try the option --http-debug, which can enable logging of all HTTP requests and responses. For example, if I run something like:

k6 run --http-debug=headers test.js
// test.js
import { sleep } from 'k6';
import http from 'k6/http';

export default function () {
  http.get('http://127.0.0.1:8085/', {
     headers: {
        "header1": null,
        "header2": '',
        "header3": "",
        "header4": undefined,
        "header5": "something"
     }
  });

  sleep(1);
}

I’ll see in the logs the following request details:

INFO[0000] Request:
GET / HTTP/1.1
Host: 127.0.0.1:8085
User-Agent: k6/0.35.0 (https://k6.io/)
Header1: null
Header2: 
Header3: 
Header4: undefined
Header5: something
Accept-Encoding: gzip

Let me know if that helps,
Cheers

Thanks for the response. When I set it to opening and closing quotes, the --http-debug flag doesn’t show it but I print the response object which prints the headers used to send the particular request where header is shown like this {“BId”:[“”]}, where as I expect it to be {“BId” : }

where as I expect it to be {“BId” : }

The thing is that the headers are just key/value pairs of the strings. And the typical way to pass multiply values is comma-separated value, like:

X-HEADER: ab,cd,wx,yz

So, interpreting the value as the array is the responsibility of the server-side. If your serverside is expecting to constantly parse value like [] in the header, you can try this:

http.get('http://127.0.0.1:8085/', {
     headers: {
        "header_empty": "[]", // it's a string
        "header_filled": ["one", "two"], // this will be converted to a CSV
        "header_filled_the_other_way": "[\"one\", \"two\"]",  // it's a string again
     }
  });

Which produces this:

GET / HTTP/1.1
Host: 127.0.0.1:8085
User-Agent: k6/0.35.0 (https://k6.io/)
Header_empty: []
Header_filled: one,two
Header_filled_the_other_way: ["one", "two"]
Accept-Encoding: gzip

Let me know if that helps!