k6: how to send post request with empty request body, content-type 'application/json' and content-length 0?

497 views Asked by At

I'm trying to send a post request using k6's http.post. It needs to have an empty request body (content length 0) and Content-Type: 'application/json'.

I tried

let res = http.post(url, {}, {
    headers: { 'Content-Type': 'application/json' },
  });

and

let res = http.post(url, null, {
    headers: { 'Content-Type': 'application/json' },
  });

and

let res = http.post(url, "", {
    headers: { 'Content-Type': 'application/json' },
  });

In all cases I got a 411 response because no content length was set. I was expecting the content length to be set to 0.

The only way I can get the request to be sent is to wrap the empty request body in JSON.stringify, e.g.

  let res = http.post(url, JSON.stringify({}), {
    headers: { 'Content-Type': 'application/json' },
  });

But this means content length of 2, rather than 0. Is there a more correct / idiomatic way to send the empty body?

0

There are 0 answers