Skip to content
Myr

Fix FormData `content-type` boundary regeneration when retrying in hooks (#752)

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

authored by Sindre Sorhus · committed by GitHub · parent 019e10e · Browse files · 2 files+69 added−8 removed
This commit

State

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

Diff stats

2 files+69 added−8 removed

  • tests:+58 added−0 removed
  • other:+11 added−8 removed

2 files+69 added−8 removed

modified:source/core/Ky.ts+11 added-8 removed
213 unmodified lines
214 this._options.body = this._options.stringifyJson?.(this._options.json) ?? JSON.stringify(this._options.json);215 this._options.headers.set('content-type', this._options.headers.get('content-type') ?? 'application/json');216 }217218 // To provide correct form boundary, Content-Type header should be deleted when creating Request from another Request with FormData/URLSearchParams body219 // Only delete if user didn't explicitly provide a custom content-type220 const userProvidedContentType = options.headers && new globalThis.Headers(options.headers as HeadersInit).has('content-type');221 if (222 this._input instanceof globalThis.Request223 && ((supportsFormData && this._options.body instanceof globalThis.FormData) || this._options.body instanceof URLSearchParams)224 && !userProvidedContentType225 ) {226 this._options.headers.delete('content-type');227 }228229 this.request = new globalThis.Request(this._input, this._options);230231 if (hasSearchParameters(this._options.searchParams)) {232 // eslint-disable-next-line unicorn/prevent-abbreviations
3 unmodified lines
236 // eslint-disable-next-line unicorn/prevent-abbreviations237 const searchParams = '?' + textSearchParams;238 const url = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, searchParams);239229 // To provide correct form boundary, Content-Type header should be deleted each time when new Request instantiated from another one230 if (231 ((supportsFormData && this._options.body instanceof globalThis.FormData)232 || this._options.body instanceof URLSearchParams) && !(this._options.headers && (this._options.headers as Record<string, string>)['content-type'])233 ) {234 this.request.headers.delete('content-type');235 }236240 // The spread of `this.request` is required as otherwise it misses the `duplex` option for some reason and throws.241 this.request = new globalThis.Request(new globalThis.Request(url, {...this.request}), this._options as RequestInit);242 }243
122 unmodified lines
modified:test/formdata-searchparams.ts+58 added