User defined tags in requests and batch?

How to use user defined tags in post request and in batch method as well ?

(question copied from k6 slack channel)

You can put a tags field in the params options as with the http.get (Tags and Groups).

You should put body in the case of http.post if it has to be empty you can put null, empty string or empty object. In the case of http.batch you can look at the documentation batch( requests ) to see how params is defined and used.

(answer copied from k6 slack channel)

@mstoykov Could you please share an example of where to put the tag in a POST request. Thanks.

Tags should be passed in the params - the third parameter to http.post.

See the example below.

import http from 'k6/http';

export default function () {
  var payload = JSON.stringify({
    email: 'aaa',
    password: 'bbb',
  });

  var params = {
    tags: {
      'RequestType': 'Login',
      'MyTag': 'MyValue',
    },
  };

  // single request
  http.post('http://test.k6.io/my_messages.php', payload, params);

  // batch
  http.batch([
      ['POST', 'http://test.k6.io/my_messages.php', payload, params],
      ['GET',  'https://test.k6.io', null, { tags: { RequestType: 'Test' } }],
    ])
}
1 Like

And if I had custom headers then request will look like the ones below?

[β€˜POST’, β€˜My messages’, payload, header, params],
[β€˜GET’, β€˜https://test.k6.io’, null, headers, { tags: { RequestType: β€˜Test’ } }],
])

No, the params object should contain both headers and tags at the same level.

Taking the example from the Params documentation and modifying it slightly:

import http from 'k6/http';
export default function () {
  let params = {
    cookies: { my_cookie: 'value' },
    headers: { 'X-MyHeader': 'k6test' },
    redirects: 5,
    tags: { name: 'my GET request' },
  };

  http.batch([
      ['POST', 'http://test.k6.io/my_messages.php', payload, params],
      ['GET',  'https://test.k6.io', null, { tags: { RequestType: 'Test' } }],
    ])
}
1 Like

Thanks @Tom, it worked