10

I have exactly angular version 4.3.2 and I cannot update because of dependencies. So for now I stick to this version. I have object with dynamic params (there can be other keys and values inside):

let query = {
    param1: 1,
    param2: 'a'
}

and I want to do something like:

params = new HttpParams();
params = params.append(query);
return this.httpClient.get(this.config.apiUrl + '/items', {
            params: params,
            headers: headers
        });

but there is not such params.append(query) method. So I need to iterate over the query keys and to add them one by one to the params. Is there easier solution for this?

Edit1:

according this answer this can be done since angular 5.0.0-beta.6 (2017-09-03), but is not solution for me. https://stackoverflow.com/a/46317900/1995258

2
  • 1
    Nope, seems like that's the way you do it: stackoverflow.com/a/45428942/3001761 Commented Oct 10, 2017 at 8:31
  • @jonrsharpe OK, thank you. I haven't found that answer. It's good reference. Commented Oct 10, 2017 at 8:36

5 Answers 5

20

It can be done by using fromObject property

const params = {
    email:'[email protected]',
    password:'qwerty'
}

const body = new HttpParams({fromObject: params})
Sign up to request clarification or add additional context in comments.

1 Comment

The value of fromObject has type restrictions: either (1) all the keys and values must be strings or (2) it must be a ReadonlyArray<string>. cf. angular.io/api/common/http/HttpParamsOptions#fromObject
6

I solved it that way:

const params: HttpParams = new HttpParams().set('filters', JSON.stringify(filters));

1 Comment

Object.keys(params).forEach(function (key) { httpParams = httpParams.append(key, JSON.stringify(params[key])); });
1

If your object is typed you cannot use fromObject since you will encounter

The value of fromObject has type restrictions: either (1) all the keys and values must be strings or (2) it must be a ReadonlyArray<string>. cf.

I've managed to create dynamically the httpParams by appending the property one by one with the following technique:

export interface NameFilter {
  sort:string,
  start:number,
  end:number,
  like:string
}

const mockFilter:NameFilter = {
    end:12,
    start:0,
    sort:"name",
    like:"test"
}

let httpParams:HttpParams = new HttpParams();
type ObjectKey = keyof typeof filter;
Object.keys(filter).forEach(
    function (key) { 
        httpParams=httpParams.append(key, 
        String(filter[key as ObjectKey]));
    }
);
return this.http.get<Name[]>(this.baseUrl+"/search",{params:httpParams})

Comments

0
    let query = {
    param1: 1,
    param2: 'a'
}
    params = new HttpParams();
    params = params.appendAll(query);
    return this.httpClient.get(this.config.apiUrl + '/items', {
                params: params,
                headers: headers
            });

You can also achive this way

Comments

0

it will be work

let params = new HttpParams();
params = params.append('param1', '1' )
params = params.append('param2', 'a' )

Get URL be like

this.HttpClient.get(this.config.apiUrl + '/items', { params:params}).subscribe(
(response)=>{
   console.log(response)
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.