-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fetch: Support object literal serialization (as JSON) #213
Comments
I'm already wrapping Converting ex: // Note: This is simplified/dumb version of https://github.com/friday/query-string-encode
function queryStringEncode(input) {
// Array of path/value tuples
var flattened = [];
(function flatten(input, path) {
// Add path and value to flat array
if ([Boolean, Number, String].indexOf(input.constructor) !== -1) {
var serializedPath = path.map(function (key, index) {
return index ? "[" + key + "]" : key;
}).join("");
flattened.push([serializedPath, input]);
}
// Iterate over next level of array/object
else if ([Array, Object].indexOf(input.constructor) !== -1) {
for (var key in input) {
flatten(input[key], path.concat([key]));
}
}
})(input, []);
// Convert array to query string
return flattened.map(function (pair) {
return pair.map(encodeURIComponent).join("=");
}).join("&");
} |
The thing is, sometimes we want to send the actual object, e.g. uploading File objects. |
Yes. Only object literals or objects created with ({}).constructor === Object // true
new Blob().constructor === Object // false
new FormData.constructor === Object // false |
Use the Bliss |
Oh yeah, I recall something about this. |
What do you mean? |
Oh, I read that too quickly. Nevermind the "as well" part of the comment. |
Is the |
Perhaps not @joyously. However Bliss's Fetch implementation is not a polyfill. It's different from the Fetch standard in small ways that means people who use it can't just drop-in replace the native browser Fetch or a polyfill. The browser support for Fetch is very good today. When the Fetch API standard was released and got initial browser support you couldn't get any upload/download progress or abort the request. AbortController and ReadableStream were added later to solve that. They're also well supported today, but this adds to more differences in the Fetch API implementation and Bliss's Fetch. However you're probably right that this issue may not be important today anymore. I'll leave that decision to Lea. |
Since #128 was fixed, the built-in object serialization was removed in favor of either diy-serialization or hooks.
I'm proposing to reintroduce object serialization, but as JSON, not query strings, because of the many issues and implementation details to settle with query strings.
Problems with query strings
"true"
vstrue
or5
vs"5"
or["a"]
vs{0:'a'}
.+
or%20
. Besides making the serializer more complex and harder to both maintain and use, this also meansfetch
would have to support these arguments as well, or only support the defaults. I think this partly defeats the purpose of supporting object serialization infetch
.JSON (
application/json
) doesn't have these problemsJSON.stringify()
is supported since IE8.On the negative side, it could be confusing if strings are treated as query strings (current behavior) but object literals are serialized as application/json (this proposal). Depending on your server environment it could also take some extra effort to support.
The text was updated successfully, but these errors were encountered: