Skip to content
Myr

Allow overriding `Content-Type` header for JSON requests (#429)

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

authored by Seth Holladay · committed by GitHub · parent d74e16d · Browse files · 4 files+43 added−4 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+43 added−4 removed

  • tests:+18 added−0 removed
  • other:+25 added−4 removed

4 files+43 added−4 removed

modified:source/core/Ky.ts+4 added-4 removed
174 unmodified lines
175 }176177 if (this._options.json !== undefined) {178 this._options.body = JSON.stringify(this._options.json);179 this.request.headers.set('content-type', 'application/json');179 this.request.headers.set('content-type', this._options.headers.get('content-type') ?? 'application/json');180 this.request = new globalThis.Request(this.request, {body: this._options.body});181 }182 }183
116 unmodified lines
300 return;301 }302303 if (onDownloadProgress) {304 transferredBytes += value!.byteLength;304 transferredBytes += value.byteLength;305 const percent = totalBytes === 0 ? 0 : transferredBytes / totalBytes;306 onDownloadProgress({percent, transferredBytes, totalBytes}, value!);306 onDownloadProgress({percent, transferredBytes, totalBytes}, value);307 }308309 controller.enqueue(value!);309 controller.enqueue(value);310 await read();311 }312313 await read();
5 unmodified lines
modified:source/types/options.ts+1 added
226 unmodified lines
227// eslint-disable-next-line @typescript-eslint/ban-types228Omit<Options, 'hooks' | 'retry'>,229'credentials' | 'fetch' | 'prefixUrl' | 'timeout'230> & {231 headers: Required<Headers>;232 hooks: Required<Hooks>;233 retry: Required<RetryOptions>;234 prefixUrl: string;235};
16 unmodified lines
modified:test/headers.ts+18 added
147 unmodified lines
148149 t.is(headers['content-length'], '0');150});151152test('json manual `content-type` header', async t => {153 const server = await createHttpTestServer();154 server.post('/', echoHeaders);155156 const headers = await ky157 .post(server.url, {158 headers: {159 'content-type': 'custom',160 },161 json: {162 foo: true,163 },164 })165 .json<IncomingHttpHeaders>();166167 t.is(headers['content-type'], 'custom');168});169170test('form-data manual `content-type` header', async t => {171 const server = await createHttpTestServer();172 server.post('/', echoHeaders);173
258 unmodified lines
modified:readme.md+20 added
523 unmodified lines
524525const response = await ky.post(url, {body: searchParams});526```527528### Setting a custom `Content-Type`529530Ky automatically sets an appropriate [`Content-Type`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) header for each request based on the data in the request body. However, some APIs require custom, non-standard content types, such as `application/x-amz-json-1.1`. Using the `headers` option, you can manually override the content type.531532```js533import ky from 'ky';534535const json = await ky.post('https://example.com', {536 headers: {537 'content-type': 'application/json'538 }539 json: {540 foo: true541 },542}).json();543544console.log(json);545//=> `{data: '🦄'}`546```547548### Cancellation549550Fetch (and hence Ky) has built-in support for request cancellation through the [`AbortController` API](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). [Read more.](https://developers.google.com/web/updates/2017/09/abortable-fetch)551
89 unmodified lines