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
Provenance
- Commit
d00864f- Tree
fd3401d- Author
Seth Holladay <me@seth-holladay.com>- Committer
GitHub <noreply@github.com>- Parent
d74e16d
4 files changed+43 added−4 removed
modified:source/core/Ky.ts+4 added-4 removed
174 unmodified lines175 }176 177 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 lines300 return;301 }302 303 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 }308 309- controller.enqueue(value!);309+ controller.enqueue(value);310 await read();311 }312 313 await read();5 unmodified linesmodified:source/types/options.ts+1 added
226 unmodified lines227 // eslint-disable-next-line @typescript-eslint/ban-types228 Omit<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 linesmodified:test/headers.ts+18 added
147 unmodified lines148 149 t.is(headers['content-length'], '0');150 });151 152+test('json manual `content-type` header', async t => {153+ const server = await createHttpTestServer();154+ server.post('/', echoHeaders);155+156+ const headers = await ky157+ .post(server.url, {158+ headers: {159+ 'content-type': 'custom',160+ },161+ json: {162+ foo: true,163+ },164+ })165+ .json<IncomingHttpHeaders>();166+167+ t.is(headers['content-type'], 'custom');168+});169+170 test('form-data manual `content-type` header', async t => {171 const server = await createHttpTestServer();172 server.post('/', echoHeaders);173 258 unmodified linesmodified:readme.md+20 added
523 unmodified lines524 525 const response = await ky.post(url, {body: searchParams});526 ```527 528+### Setting a custom `Content-Type`529+530+Ky 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.531+532+```js533+import ky from 'ky';534+535+const json = await ky.post('https://example.com', {536+ headers: {537+ 'content-type': 'application/json'538+ }539+ json: {540+ foo: true541+ },542+}).json();543+544+console.log(json);545+//=> `{data: '🦄'}`546+```547+548 ### Cancellation549 550 Fetch (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