Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular4: Http -> HttpClient - requestOptions

Tags:

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 :(

like image 461
DS_web_developer Avatar asked Aug 21 '17 12:08

DS_web_developer


People also ask

What is HttpClient used for?

An HttpClient can be used to send requests and retrieve their responses. An HttpClient is created through a builder . The builder can be used to configure per-client state, like: the preferred protocol version ( HTTP/1.1 or HTTP/2 ), whether to follow redirects, a proxy, an authenticator, etc.

What is RequestOptions in Angular?

Angular RequestOptions instantiates itself using instances of Headers , URLSearchParams and other request options such as url, method, search, body, withCredentials, responseType. These classes are imported from @angular/http API. Finally Http. get() uses instance of RequestOptions to interact with the server.

What is HttpClient request?

An HTTP request is made by a client, to a named host, which is located on a server. The aim of the request is to access a resource on the server. To make the request, the client uses components of a URL (Uniform Resource Locator), which includes the information needed to access the resource.


1 Answers

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

like image 180
Andrei Matracaru Avatar answered Sep 17 '22 11:09

Andrei Matracaru