Alex
Alex

Reputation: 6099

http.request does not care about options parameter when url is of type Request and not string in Angular

I noticed that when calling http.request

request(url: string|Request, options?: RequestOptionsArgs): Observable<Response>

the options?: RequestOptionsArgs parameter seems to be ignored if the url: string|Request parameter is of type Request and not string. If it is type Request then this overrides any headers set on the optional options parameter.

Angular version: 2.4.2

Can anyone confirm this? Is this intended behaviour or a bug?

Upvotes: 3

Views: 471

Answers (1)

eko
eko

Reputation: 40647

That seems to be correct,

 request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
    let responseObservable: any;
    if (typeof url === 'string') {
      responseObservable = httpRequest(
          this._backend,
          new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url)));
    } else if (url instanceof Request) {
      responseObservable = httpRequest(this._backend, url);
    } else {
      throw new Error('First argument must be a url string or Request instance.');
    }
    return responseObservable;
  }

Source code: https://github.com/angular/angular/blob/master/packages/http/src/http.ts#L113

Upvotes: 1

Related Questions