Randomize the order of headers in an HTTP request

If you want to randomize the order of headers that you put in an HttpRequest block, you can write the headers to a list, shuffle the list, convert it to a dictionary and then use the resulting variable in the HttpRequest block.

Please see the sample code below

BLOCK:ConstantList
  value = ["one: 1", "two: 2"]
  => VAR @headersList
ENDBLOCK

BLOCK:Shuffle
  list = @headersList
ENDBLOCK

BLOCK:ListToDictionary
  list = @headersList
  => VAR @headersDict
ENDBLOCK

BLOCK:HttpRequest
  url = "https://httpbin.org/anything"
  customHeaders = @headersDict
  TYPE:STANDARD
  $""
  "application/x-www-form-urlencoded"
ENDBLOCK

IMPORTANT NOTE
Some headers (e.g. Host, Content-Type and Content-Length) are automatically added by OB2 in certain places. If you want to override the order of those headers, you need to manually add them to the initial list and not let OB2 generate them automatically. If OB2 finds those headers in the dictionary you supplied as custom headers, it will use the order you provided.

In the case of the Content-Length header you will need to manually calculate the length of your content (using the Length block under string functions) and put the correct value, otherwise you will get an Operation Canceled error when you try to send the request.

3 Likes