Skip to content

Commit

Permalink
Represent non-stringified JSON request body as an [object Object] string
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored and JakeChampion committed Feb 17, 2021
1 parent b5c8bd0 commit 5c6b055
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion fetch.js
Expand Up @@ -246,14 +246,20 @@ function Body() {
this._bodyText = body = Object.prototype.toString.call(body)
}

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') {
// 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

This comment has been minimized.

Copy link
@hans2520

hans2520 Feb 23, 2021

This line is causing empty body to be sent on POST in Electron 80 (https://www.electronjs.org/) used by Cypress 4.

Commenting this line out fixes things, another approach that works is to store the original body before the conditional branches, and assign that here, instead of the this._bodyText that has been modified by the branches. I tested that as well and it works.

This comment has been minimized.

Copy link
@JakeChampion

JakeChampion Feb 23, 2021

Owner

will you be submitting a pull-request?

This comment has been minimized.

Copy link
@hans2520

hans2520 Feb 24, 2021

@JakeChampion not sure if that's a good fix, probably should rework the branching logic

}
}

Expand Down

2 comments on commit 5c6b055

@Taranehpasha
Copy link

Choose a reason for hiding this comment

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

@JakeChampion
Copy link
Owner

Choose a reason for hiding this comment

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

@Taranehpasha can you explain why you posted those links?

Please sign in to comment.