Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,20 @@ function Body() {
this._bodyText = body = Object.prototype.toString.call(body)
Copy link
Contributor Author

@kettanaito kettanaito Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we can set this._bodyInit to equal this._bodyText in this closure. It fixes the issue, but the solution becomes less deterministic not being connected with the request's content-type. I'm not sure it would be a good idea to always reset the body to its textual counterpart, but I would appreciate some feedback.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this._bodyText

}

if (!this.headers.get('content-type')) {
const contentType = this.headers.get('content-type')

if (!contentType) {
if (typeof body === 'string') {
this.headers.set('content-type', 'text/plain;charset=UTF-8')
} else if (this._bodyBlob && this._bodyBlob.type) {
this.headers.set('content-type', this._bodyBlob.type)
} else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8')
}
} else if (contentType.includes('json') && typeof this._bodyInit !== 'string') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a reliable JSON request body content-type header check. Would you recommend to provide application/json; charset=UTF-8 instead?

I'm thinking that this behavior should also affect any JSON-like content-type header (i.e. application/hal+json).

// Always pass a text representation of a non-stringified JSON body
// to `XMLHttpRequest.send` to retain a compatible behavior with the browser.
this._bodyInit = this._bodyText
}
}

Expand Down