25

So, I am trying to migrate from 'old' http to the new httpClient

with the http client I am using this format in my service:

return this.http.get(environment.api+ '.feed.json', requestOptions)

how do I use this in httpClient?

tried many thiungs... including

return this.http.get(environment.api+ '.feed.json', {params: requestOptions.params})

but getting a type missmatch :(

1 Answer 1

22

Try this:

const requestOptions = {
  params: new HttpParams()
};

requestOptions.params.set('foo', 'bar');

this.http.get(environment.api+ '.feed.json', requestOptions );

Here is also the link to the docs describing how to do that with examples for headers and URL Parameters: HttpClient

Sign up to request clarification or add additional context in comments.

6 Comments

so I cannot use const requestOptions = new RequestOptions(); anymore?
no, RequestOptions is part of the @angular/http package - see here
the HttpParams() are really weird.... you cannot do like you did @AndreiMatracaru :/ you have to concatenate like new HttpParams().set('foo', 'bar') otherwise is not working.... and I don't know how to use it if I have many params I need to add but validate first :/
@DS_web_developer, in case if you have to do some things with your params and add them after this, you can do something like that: let params = new HttpParams(); params = params.append('foo', 'bar').append('baz', 'qux'); And so on.
How might I use your example to set withCredentials: true?
|

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.