Angular's Http
module used to have a RequestOptions
object which could be passed to its get
method (e.g. this.http.get(fooUrl, barOptions)
. The RequestOptions
contained any headers. This was deprecated. The "closest replacement" for the deprecated RequestOptions
is HttpRequest.
There's two problems with this:
1. HttpRequest
has a url
param, so it is NOT an equivalent of RequestOptions
. In fact, it seems to encapsulate the entire request, yet I don't see it used anywhere, so I am guessing it's just an internal class..
2. HttpClient
's get
method does NOT take HttpRequest
as an argument.
The source code for HttpClient
's get
method looks like this:
/**
* Constructs a `GET` request that interprets the body as an `ArrayBuffer` and returns the response in
* an `ArrayBuffer`.
*
* @param url The endpoint URL.
* @param options The HTTP options to send with the request.
*
* @return An `Observable` of the response, with the response body as an `ArrayBuffer`.
*/
get(url: string, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType: 'arraybuffer';
withCredentials?: boolean;
}): Observable<ArrayBuffer>;
Is s there some type I can use for the options
param now? Should I just define an interface that has HttpHeaders on it? If there isn't a type for it, why not?
My old code looks like this:
reqOptions.headers.set('Authorization', user.token);
reqOptions.headers.set('RefreshToken', user.refreshToken);
RequestOptions
from the old Http
module had a headers property and was statically typed.
P.S. I read the answer here, but it doesn't use a type: Angular4: Http -> HttpClient - requestOptions
HttpClient
across the board. – Pejorative