Send URLSearchParams as request body without extra configuration (#2695)

This commit is contained in:
James Cote 2022-01-31 04:12:02 -05:00 committed by GitHub
parent ab4b21dacc
commit 99c6d813d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 3 deletions

View file

@ -76,7 +76,7 @@ module.exports = function($window, Promise, oncompletion) {
request: makeRequest(function(url, args, resolve, reject) {
var method = args.method != null ? args.method.toUpperCase() : "GET"
var body = args.body
var assumeJSON = (args.serialize == null || args.serialize === JSON.serialize) && !(body instanceof $window.FormData)
var assumeJSON = (args.serialize == null || args.serialize === JSON.serialize) && !(body instanceof $window.FormData || body instanceof $window.URLSearchParams)
var responseType = args.responseType || (typeof args.extract === "function" ? "" : "json")
var xhr = new $window.XMLHttpRequest(), aborted = false, isTimeout = false
@ -194,7 +194,7 @@ module.exports = function($window, Promise, oncompletion) {
if (body == null) xhr.send()
else if (typeof args.serialize === "function") xhr.send(args.serialize(body))
else if (body instanceof $window.FormData) xhr.send(body)
else if (body instanceof $window.FormData || body instanceof $window.URLSearchParams) xhr.send(body)
else xhr.send(JSON.stringify(body))
}),
jsonp: makeRequest(function(url, args, resolve, reject) {

View file

@ -212,6 +212,16 @@ o.spec("request", function() {
o(data).deepEquals({a: "/items", b: [{x: "y"}]})
}).then(done)
})
o("works w/ URLSearchParams body", function(done) {
mock.$defineRoutes({
"POST /item": function(request) {
return {status: 200, responseText: JSON.stringify({a: request.url, b: request.body.toString()})}
}
})
request({method: "POST", url: "/item", body: new URLSearchParams({x: "y", z: "w"})}).then(function(data) {
o(data).deepEquals({a: "/item", b: "x=y&z=w"})
}).then(done)
});
o("ignores unresolved parameter via GET", function(done) {
mock.$defineRoutes({
"GET /item/:x": function(request) {