Unsupported media type - http 415

Hi folks. I am getting a weird encoding error from my script which makes nonsense. in a nutshell the error code is 415 unsupported media type. which makes no sense because my content-type is explicitly set.

within a function() the below are set

 const params = {
        headers: {
            'Authorization': 'Bearer ' + api_token,
            'Content-Type': 'application/json', // Content-Type is being ignored and payload is being decoded as x-www-form-urlencoded which is not supported. Check with Dev
        },
    const data = JSON.stringify({
        "stuff": `${stuff}`,
         .....
    });
return http.post(url, params, data); produces 415 error

however at k6 runtime I get the following error log which shows the the content-type was translated as something other than application/json. also the header is encoded?? why does the body in the request contain the header key/values?

  "error": "",
  "error_code": <bad error#>,
  "request": {
    "method": "POST",
    "url": "http://url.com",
    "headers": {
      "Content-Type": [
        "application/x-www-form-urlencoded"
      ],
      "User-Agent": [
        "k6/0.37.0 (https://k6.io/)"
      ]
    },
    "body": "headers=map%5BAuthorization%3ABearer+"<data-redacated>"+Content-Type%3Aapplication%2Fjson%5D",
    "cookies": {}
  }
}

when I swap the function inputs I get 400 errors? which is better than 415 of course. How do I find out the order for values passed into the http class/function.

return http.post(url, data, params); produces 400 error

Asked and answered… - post( url, [body], [params] ) which solves my problem with media type and http.post class input order.

However any comments are welcome :hugs:

You beat me to it!

A HTTP 400 “Bad Request” typically means there’s something wrong with the POST data, i.e. your JSON payload. It would be difficult to tell you what exactly that might be (we’d need to reproduce the problem, and ideally also see a working example).