-
Notifications
You must be signed in to change notification settings - Fork 25k
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
by default not send undefined params in url #20564
Comments
Also this is useful for making generic HTTP services instead of creating one with 2 params & 3 params etc.
|
@istiti I am in a similar need. Mine is an angular 5 + dotnet core web api application I believe you might already have a workaround. Here is my setup looks like. And i get 'undefined' for fName and lName query parameters at my dotnet core Web Api backend. You can assume fName and lName are optional. params:{ firstname:'', lastName:'' } this.http.get(`/user?fName=${this.params.firstname}&lName=${this.params.lastName}`); I even tried this but ended up with same result. params:{ firstname:" ", lastName:" " } this.http.get(`/user?fName=${this.params.firstname}&lName=${this.params.lastName}`); How are you handling the 'undefined' from being passed to your backend..? I could not find a way to assign an empty string value to my object properties. Appreciate your input. Thanks! |
Duplicate of #18567 |
I believe that the way it works now has some inconsistencies:
console prints out: g=undefined&n=null', null, null, null, undefined, null |
Since I use optional parameters quite a bit, I decided to write my own helper function which I have listed below in case it proves useful to others. In a file named http-params.ts:
Then, when I need to create some HTTP parameters, I use it as follows:
|
@Mufasa, I found great success in using the helper function you've created after beginning the migration to the Angular 4.4 HttpClient from Angular 2's Http in our code base. Many requests we're making rely on the same optional parameter behavior from the previous |
@timtheguy I'm glad it came in useful. |
Thanks for your helper function @Mufasa :) It's very useful! |
We too seek that functionality (and believe it should come out of the box) |
This definitely should be the default behavior. |
For future readers, this can also be handled generically as an HTTP Interceptor:
|
Or if you prefer a function to deal with objects ( multidimensional ) before sending it to HttpClient. Feel free to suggest for more optimization.
|
Impressive that this still seems to be an issue with Angular 11. Glad people made workaround here at last. |
FWIW- I don't think it should be the framework's responsibility to protect us from passing invalid values. If I pass an undefined by accident, I would much rather it show up in the URL so I know exactly what I'm doing wrong. If these values disappear 'magically', and I'm actually expecting them, I face a much harder task tracing the origin. |
Personally, I think this is a perfect case for an interceptor as suggest in #20564 (comment). Exactly the sort of thing that this extension point was designed for. Then, rather than having it built-in and hidden behind a config option, it is triggered by adding the interceptor or not. |
I would at least expect a property inside HttpParamsOptions. For example: Default behaviour might be that it does not strip undefined. An interceptor works, but shouldn't be needed for something this simple :) |
up please |
NOTE: This would strip out all falsey values, including
|
The interceptor workaround appears to no longer work, since any
That is, the value has been already changed to a string before the The change was possibly introduced with this commit https://github.com/angular/angular/blame/ec8b52af69a2e04a7b077eb4d4309870ba40e273/packages/common/http/src/params.ts#L168 , which will ensure all values stored with |
@jmbe same here, we had a also implemented an interceptor that doesn't work anymore. |
My solution is to add a new util method to remove "empty" object properties. And it can be used not only for http params. /**
* Removes empty properties from an object
*
* @param object is any object
* @returns a clone of the given object with no empty (`undefined` or `null`) properties
**/
static removeEmptyProperties(object) {
return _.pick(object, value => value !== null && value !== undefined);
} We use the That's how I can use it: find$(searchItem): Observable<any> {
const url = `${this.URL_BASE}/find/`;
const params = AppUtil.removeEmptyProperties(searchItem);
return this.http.get<any>(url, {params});
} |
Seven years and still counting... Maybe you should align with the behavior how JSON values are handled in JavaScript: {a: undefined} is often used as a construct to ignore a JSON key. Maybe Here is how They remove both |
I'm submitting a...
CURRENT
To be simple, I will take example for GET request. Here you have a simple method making http call with an optional parameter:
cityId?
is optional, so you can call method likethis.getCity()
, if you do so angular will make request to this url :http://localhost/city?city=undefined
EXPECTED
I think avoid sending undefined params (not including them in HttpParams if they are undefined) should be most common use case because we can check this on backend. If params aren't present == undefined.
MOTIVATION
Advantages of this is : maybe URL have limits and sending essential/useful data we really need is important. I don't understand why send them by default if they are undefined. Something like Include.NON_NULL in jackson.
What you think about not send them by default ? or add option in options{} object just to avoid this big codes:
Otherwise yes actually we can fortunately workaround by interceptor or with :
But when you have 10/20 params or 500/1k http request you are happy if you avoid this condition :-D
I didn't think about others backend endpoint so will think within my use case. Idea should only react with undefined params not with null params. For instance my endpoint in java if I expect to receive
@PathVariable Long cityId
I can receive null but not undefined.Environment
Angular 5.0.2
Browser tested:
The text was updated successfully, but these errors were encountered: