HttpParams : Handling of null & undefined values

See original GitHub issue

I’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.

  • null and undefined are converted to ""? or;
  • null is "", and undefined is 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:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
robwormaldcommented, Aug 7, 2017

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

let q = new URLSearchParams();
q.append('x', null);
q.append('y', undefined);
q.append('z', '');

q.toString(); // x=null&y=undefined&z=

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.

9reactions
ChrisDevinePimsscommented, Jan 25, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found