HttpParams : Handling of null & undefined values
See original GitHub issueI’m submitting a…
[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request
Current behavior
new HttpParams().set('arg', null).toString(); // "arg=null"
let a = {};
new HttpParams().set('arg', a.any).toString(); // "arg=undefined"
Expected behavior
new HttpParams().set('arg', null).toString(); // "arg="
let a = {};
new HttpParams().set('arg', a.any).toString(); // "arg="
Minimal reproduction of the problem with instructions
https://github.com/angular/angular/blob/master/packages/common/http/src/params.ts#L57
This is due to the underlying encodeURIComponent return string literals for “null” or “undefined”;
(edited) Plnkr replication : https://embed.plnkr.co/k5naIib3sGADM91UZ31a/
What is the motivation / use case for changing the behavior?
This has mostly arisen due to HttpClient, and replacement of URLSearchParams, with HttpParams
Is this an intended behaviour, or does this require a fix? e.g.
nullandundefinedare converted to""? or;nullis"", andundefinedis thrown?
I appreciate these are not intended as direct replacements, and you can create your own HttpParameterCodec, but this seems like an unintended operation.
Environment
Angular version: 4.3.3
Browser:
- [x] Chrome (desktop) version 60.0
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Remove null params in HttpParams - angular - Stack Overflow
You should filter them client side using JavaScript's delete (MDN) keyword if they have a value of undefined (which is different than the ......
Read more >HttpParams - Angular
Returns. string | null : The first value of the given parameter, or null if the parameter is not present.
Read more >Argument type 'string or null' not assignable to parameter of ...
The error "Argument of type 'string | null' is not assignable to parameter of type string" occurs when a possibly `null` value is...
Read more >URLSearchParams - Web APIs - MDN Web Docs
Chrome Edge
URLSearchParams Full support. Chrome49. Toggle history Full support. Edge...
@@iterator Full support. Chrome49. Toggle history Full support. Edge...
URLSearchParams() constructor Full support. Chrome49. Toggle...
Read more >Communicating with backend services using HTTP - Angular
Use the params property to configure a request with HTTP URL parameters, ... body: undefined }); // preserve original body newReq = req.clone({...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is the intended behavior, I believe, as it tracks the behavior of the native-standard - https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
Since there’s no way to encode a null/undefined value to a string, the behavior is sort of undefined 😃 - best to pass an empty string if that’s what you want to send.
Didn’t the old one allow to pass nulls/undefined and it would not put them in the query string.
I only noticed because iv recently updated to use HttpClient and found this change in functionality has broken a lot of services.