angular / angular/angular

Please let HttpParams.set and .append accept one object-like arg with nested data

Open
#20,298 14 comments 15 reactions 0 assignees View on GitHub
area: common/http feature feature: under consideration P4
Dominant language
TypeScript
Stars
101k
Forks
27.5k
Avg merge
1d 19h
Merged PRs (30d)
288

Description

## I'm submitting a...
[ x ] Feature request

## Current behavior

You can only set or append to HttpParams one by one.

## Expected behavior

With Http.get, .post, etc, you could pass any object with nested object-like data as options.params. First level keys would be mapped to query params, and values JSON encoded if they're object-like. I'd like to achieve the same without adding complexity by doing something like:

> new HttpParams.set( { where: { id: [ 1,2,3 ] } } )

## What is the motivation / use case for changing the behavior?

In migrating to HttpClient from Http, I had to add a helper method:
```
private anyToHttpParams(obj: any) { // requires lodash
return Object.entries(obj)
.reduce(
(params, [key, value]) => params.set(
key, _.isObjectLike(value) ? JSON.stringify(value) : value
),
new HttpParams()
);
}

// this.http.get(url, { params: this.anyToHttpParams({ where: { id: [ 1,2,3 ] } }) } );
```

For me, this was sort of a breaking change as well, as I didn't get a compiler error when I just replaced Http with HttpClient and passed objects with nested data to params like I used to with Http, but in network console my xhr URLs changed to something like `/action?[object object]`.

## Environment

Angular version: 5.0.1

Contributor guide

Open the contributing guide

Research direction

Start with the HttpParams.set and append APIs and the HttpClient options.params path described in the issue. Done means one object-like argument maps first-level keys to query parameters and JSON-encodes object-like values, including nested data.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.