Skip to content
Myr

Fix `json` option keeping the content-type inherited from a `Request` input body

Before adoption: this commit came in with the history the project adopted, so no landing records it.

A `Request` input constructed with a body gets an automatic `text/plain;charset=UTF-8` content-type. That header was merged in before the `json` option set its own, so the JSON body was sent with the wrong content-type. Only a content-type explicitly passed via `options.headers` now takes precedence over `application/json`.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 8950864 · Browse files · 4 files+23 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+23 added−2 removed

  • tests:+13 added−0 removed
  • other:+10 added−2 removed

4 files+23 added−2 removed

modified:source/core/Ky.ts+6 added-2 removed
425 unmodified lines
426 // @ts-expect-error - Types are outdated.427 this.#options.duplex = 'half';428 }429430 // A `Request` input's headers are already merged in, so only a content-type from `options.headers` counts as user-provided.431 const userProvidedContentType = options.headers && new globalThis.Headers(options.headers as HeadersInit).has('content-type');432433 if (this.#options.json !== undefined) {434 this.#options.body = this.#options.stringifyJson?.(this.#options.json) ?? JSON.stringify(this.#options.json);435 if (!userProvidedContentType) {432 this.#options.headers.set('content-type', this.#options.headers.get('content-type') ?? 'application/json');436 this.#options.headers.set('content-type', 'application/json');437 }438 }439440 // To provide correct form boundary, Content-Type header should be deleted when creating Request from another Request with FormData/URLSearchParams body441 // Only delete if user didn't explicitly provide a custom content-type437 const userProvidedContentType = options.headers && new globalThis.Headers(options.headers as HeadersInit).has('content-type');442 if (443 this.#input instanceof globalThis.Request444 && ((supportsFormData && this.#options.body instanceof globalThis.FormData) || this.#options.body instanceof URLSearchParams)445 && !userProvidedContentType
777 unmodified lines
modified:source/types/options.ts+2 added
40 unmodified lines
41 /**42 Shortcut for sending JSON. Use this instead of the `body` option.4344 Accepts any plain object or value, which will be stringified using `JSON.stringify()` and sent in the body with the correct header set.4546 The `Content-Type` header is set to `application/json` unless you set a `Content-Type` in the `headers` option, which always takes precedence. A `Content-Type` on a `Request` input is replaced, because the `Request` constructor sets one automatically from its body.47 */48 json?: unknown;4950 /**
435 unmodified lines
modified:test/headers.ts+13 added
168 unmodified lines
169170 t.is(headers['content-type'], 'custom');171});172173test('json option overrides `content-type` inherited from a Request input body', async t => {174 const server = await createHttpTestServer(t);175 server.post('/', echoHeaders);176177 const headers = await ky(new Request(server.url, {method: 'POST', body: 'plain'}), {178 json: {179 foo: true,180 },181 }).json<IncomingHttpHeaders>();182183 t.is(headers['content-type'], 'application/json');184});185186test('form-data manual `content-type` header', async t => {187 const server = await createHttpTestServer(t);188 server.post('/', echoHeaders);189
275 unmodified lines
modified:readme.md+2 added
206 unmodified lines
207Type: `object` and any other value accepted by [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)208209Shortcut for sending JSON. Use this instead of the `body` option. Accepts any plain object or value, which will be stringified using `JSON.stringify()` and sent in the body with the correct header set.210211The `Content-Type` header is set to `application/json` unless you set a `Content-Type` in the `headers` option, which always takes precedence. A `Content-Type` on a `Request` input is replaced, because the `Request` constructor sets one automatically from its body.212213##### searchParams214215Type: `string | object<string, string | number | boolean | undefined> | Array<Array<string | number | boolean>> | URLSearchParams`\216Default: `''`
1662 unmodified lines