sindresorhus / ky

🌳 Tiny & elegant JavaScript HTTP client based on the Fetch API
MIT License
11.91k stars 342 forks source link

[help] trying to understand why headers are not set properly #537

Closed m8uz closed 8 months ago

m8uz commented 9 months ago

I'm far down a bug rabbit hole so I may have lost my mind already.

This is the request:

  const url = "https://httpbin.org/post"
  const json = await ky
    .post(url, {
      headers: { "x-test-header": "123", "user-agent": "foo-user", "accept": "anything test", "content-type": "foo" },
      json: { myData: "12345" },
    })
    .json()
  console.log(json)

Using httpbin to inspect sent headers. This is what I'm getting back (should be a reflection of the request sent):

{
  args: {},
  data: '{"myData":"12345"}',
  files: {},
  form: {},
  headers: {
    Accept: 'application/json',
    'Accept-Encoding': 'br, gzip, deflate',
    'Accept-Language': '*',
    'Content-Length': '18',
    'Content-Type': 'text/plain;charset=UTF-8',
    Host: 'httpbin.org',
    'Sec-Fetch-Mode': 'cors',
    'User-Agent': 'node',
    'X-Amzn-Trace-Id': ...
  },
  json: { myData: '12345' },
  origin: ...,
  url: 'https://httpbin.org/post'
}

Why are the headers not sent according to my request? What am I doing wrong? This is causing requests to an internal API to fail because the content-type is incorrect. I'm using Ky 1.1.0. Please help, losing my sanity quickly over this.

node --version  ✔  09:19:49 v20.8.1

m8uz commented 9 months ago

duplicate of #535