-
-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Why are null and undefined variables removed from params #1139
Comments
Its the right behaviour, more strictly it should actually generate something as |
Without an equals to me should denote a boolean true. The fact that a property exists on the query object to me means that you want something to be passed through. If you don't want something in your query string then surely you should not have that property on your |
A variable without value is called a declaration which is of type undefined, which actually evaluates in false. If you would evaluate an empty string '' it also evaluates in false, but they are not equivalent since they differ in type, the prior is undefined, the latter is of type string. I think I agree with you, but it would be wrong to assume what that something should be. Assuming an empty string would be wrong, but with |
Passing a key with no value as a URL parameter is a perfectly valid use case.
is a valid URL. If no value is passed for the key, it should just leave off the |
The current behavior is actually in line with how JSON values are handled in JavaScript: {a: undefined} is often used as a construct to ignore a JSON key. Maybe {a: null} should be interpreted as "?a" param? But not undefined. |
@ahtik 👍 for
|
What about a web APIs which expects a payload parameter to be explicitly set to null? In my opinion, axios should not remove parameters with |
you can import |
Is the |
@abodelot So how do these APIs expect |
@abodelot by payload and its null-handling, are we still talking about the URL parameters, not the POST/PATCH/etc payload? I'd think |
Pull request is up. (#1987) |
@Radiergummi In a JSON payload, I'd expect an actual I'm afraid using @vuoriliikaluoma thank you for taking care of this issue |
All of these are possible: |
Why wouldn't you just allow anything to be send as the key? I think it's pretty ridiculous to remove |
You may be confusing query strings with body payload here. The object in question will be serialized into a query string. Using a literal |
Actually axios interceptor can do the trick. I faced same scenario where json payload contained I added an interceptor as below snippet. This also works for nested object ;) thanks recursion! /**
* Add json cleaner to set undefined/null values to empty string.
* This is to prevent axios(json to string using jsonify) from removing those keys
* when converting json paylod to string.
*/
function cleanJSON(json) {
for (let key in json) {
if (json[key] === undefined || json[key] === null) {
json[key] = '';
} else if (typeof json[key] === 'object')
json[key] = cleanJSON(json[key]);
}
return json;
}
// Add a request interceptor
window.axios.interceptors.request.use(
function(config) {
if (['post', 'put', 'patch'].includes(config.method)) {
config.data = cleanJSON(config.data);
}
return config;
},
function(error) {
return Promise.reject(error);
}
); |
I actually like the default behavior, since it's query string: { key: undefined } to ? From a strictly typed perspective, only string is a valid query string (see... it's a self validating statement 😄 ).. |
@Munawwar that's not how HTTP works. The first two are valid HTTP query parameters and should not be removed from the query string just because they have no value for the key. |
HTTP spec does not dictate null and undefined serialization (the latter of which is a JS only thing). There is a way to achieve each of the url param representation.. let me inverse the example To achieve: |
That's not the same thing, and we're not talking about serialization. |
Oh didn't think of that one.. I'll keep quite now 🤐 so null should do that but undefined shouldn't yes? |
No, it would generally be used to translate into a Boolean or a state of some kind. |
Just wasted 30 minutes of debugging to discover that axios deletes fields with empty strings ("") in the payload. If that's not a bug I don't know what is. |
@fredaas I just tried adding an empty string into one of the keys in |
@PeterDKC Very true. I think what If a key includes the Is passing a query string parameter (whose value is null) without the equals sign valid? |
is there any progress? |
Why is this still an issue. Was able to work around by providing a default param serializer in the client code: const defaultParamsSerializer = (
params: Record<string, unknown>,
options?: ParamsSerializerOptions,
//@ts-ignore
) => new URLSearchParams(params).toString();
export const ApiClient = axios.create({
baseURL: '/api',
headers: {
'Content-Type': 'application/json',
},
paramsSerializer: { serialize: defaultParamsSerializer },
withCredentials: true,
}); |
is there any progress? |
4 years after my last comment, and here me on this, I think it's fair to keep it as is. I think it goes into the study of what defining "nothing" means, and what Took me a long time to figure out why, but it has to do with the understanding of what "void" really means. |
@mrjackyliang Hard disagree. Keys with no values are and always have been a valid part of the HTTP spec. This intervenes and breaks otherwise valid functionally. That's incorrect behavior and always has been. |
In what way? Can you give an example of your use case? Under HTTP spec, keys with no values are valid, but if you're trying to convert However, nobody is stopping you from putting an empty string as a value, in which, is correct from how I see it, and HTTP params are of |
@mrjackyliang Sometimes need to pass param=null it means on api that filter by this property which in database null. If you don’t pass then not needed filter this field by null. |
If that's the case, and you're passing But, if you're up for some fun, using Zod to parse web requests is actually very neat. |
Null is stripped out by axios and isn't sent as a query parameter for the server to parse; that's the point of this issue. |
@rcbevans If i right understood. He said to pass null as string "null" |
@rcbevans Yes, then you should convert the How does it make sense to send When you send JSON over HTTP, you're not really sending actual objects. That's why there is Yes, Also, I know many developers like to do things in a shortcut manner (I do too, that's why I initially disliked it), but this isn't about how fast you can implement code, it's how accurate and fast you can implement code, which in itself is a contradiction. |
^ That's a valid HTTP param line that's been accepted by servers all over the Internet since always. The Axios is proactively taking an action (removing the key from the parameter bag) that the developer doesn't intend and the server would not have an issue with. That's what we're talking about and always have been talking about. |
I don't think it's Axios inserting an opinion. It's a problem with 2 solutions that don't agree with each other. |
After all these years it should have been implemented as an option that anyone can switch on or off. |
I mean, nobody's stopping anyone from creating a workaround as a package. |
I am already using a workaround, still, I think that it should be an option you can just turn on if you prefer. |
I mean, who doesn't? But I do believe this has to do more with economics. Why cover everything in one product when you can have other developers extend functionality for you? Just the same reason why |
axios params에서 undefined는 자동으로 제외됨 { key: undefined } to ? (not included) { key: null } to ?key { key: '' } to ?key= { key: 'null' } to ?key=null axios/axios#1139
Hello, i just stumbled on this page when looking for axios behavior with undefined values and I don't understand some propositions made here so i'm adding my two cents without meaning to add oil on the fire. The question of shortening an url doesn't feel like it's the responsability of axios to me. You pass parameters, you get them serialized. Also i did not understand the idea of "null" instead of null, that would make deserialization on backend side even more confusing, would it not? A string is a string, not a transport method and moreover, there exists languages where null is written as nil and perhaps there are some other words as well. I think omitting the value is a good choice for compatibility reasons. I guess it would be possible to add an option to set a wanted value when the passed parameter is null though. I do agree that empty strings should be passed but i don't see much their use aside from a user forgetting to fill a field 🤔 Here again no oil on the fire. Just my two cents. |
@julichan This has nothing to do with values in JS. It has everything to do with how urls are interpreted on the server layer & the HTTP spec.
Again, nothing to do with |
@PeterDKC , i do understand this, just added my two cents and for the info i called url?value a flag. Your point doesn't give me an explanation on why this should be changed so i still stand with my point and agree to disagree with you through i'm not one to make any decision here. Look at the big picture, not just the http spec. 🤣 |
If you read my explanation and don't understand why it should be changed then maybe you should read it a few more times because I literally stated what it breaks and why it needs to be changed, and it has nothing to do with either of your two cents. |
Relax @PeterDKC, this is not a war... |
@julichan Yes, you're right. It's a very simple straightforward issue that has been explained ad nauseum for years in this thread, and it should have been resolved years ago. |
if someone would like to open a PR please do so, but for now I'm closing this its super old |
axios/lib/helpers/buildURL.js
Line 38 in 638804a
I would expect
To construct
?a=&b=
The text was updated successfully, but these errors were encountered: