angular-httpclient

Preserve the order of the http calls in angular

China☆狼群 提交于 2019-12-01 10:48:55
I'm trying to solve the order problem I'm facing with several approaches that I found here on SO without a success. I have a method, where I'm loading some data for an array of leaflet layers: private loadSelectedTileLayersCapabilities(): void { let tempTileLayer; this.selectedTileLayerIds.forEach( (selectedTileLayer: string) => { tempTileLayer = this.getTileLayerById(selectedTileLayer); this.capabilitiesService.getTileLayerDimensions(tempTileLayer.url, tempTileLayer.name, tempTileLayer.id) .subscribe( dimensions => this.displayNewTileLayer(dimensions) ); } ); } And then I have a method, where

Why httpclient in angular 4 is assuming that request am sending json data

痞子三分冷 提交于 2019-12-01 10:37:34
I am using angular 4 http client to a server which returns text data so for that I did something like below this.http.get('assets/a.txt').map((res:Response) => res.text()).subscribe((data: any) => { console.log(data.text()); }); I did not tell it any where that the response is in json format but still it is raising the below error SyntaxError: Unexpected token a in JSON at position 0 at Object.parse () at XMLHttpRequest.onLoad On what basis these people assume as the response is in json format :) Gregor Doroschenko The Response of new HttpClient is JSON by default. If you want to get the

Preserve the order of the http calls in angular

寵の児 提交于 2019-12-01 09:38:52
问题 I'm trying to solve the order problem I'm facing with several approaches that I found here on SO without a success. I have a method, where I'm loading some data for an array of leaflet layers: private loadSelectedTileLayersCapabilities(): void { let tempTileLayer; this.selectedTileLayerIds.forEach( (selectedTileLayer: string) => { tempTileLayer = this.getTileLayerById(selectedTileLayer); this.capabilitiesService.getTileLayerDimensions(tempTileLayer.url, tempTileLayer.name, tempTileLayer.id)

Why do most classes of the HttpClient API define immutable objects?

我的未来我决定 提交于 2019-12-01 08:05:12
The documentation for HttpClient states the following about immutability: Interceptors exist to examine and mutate outgoing requests and incoming responses. However, it may be surprising to learn that the HttpRequest and HttpResponse classes are largely immutable. This is for a reason: because the app may retry requests, the interceptor chain may process an individual request multiple times. If requests were mutable, a retried request would be different than the original request. Immutability ensures the interceptors see the same request for each try. I find it hard to understand this

Why do most classes of the HttpClient API define immutable objects?

瘦欲@ 提交于 2019-12-01 05:42:19
问题 The documentation for HttpClient states the following about immutability: Interceptors exist to examine and mutate outgoing requests and incoming responses. However, it may be surprising to learn that the HttpRequest and HttpResponse classes are largely immutable. This is for a reason: because the app may retry requests, the interceptor chain may process an individual request multiple times. If requests were mutable, a retried request would be different than the original request. Immutability

Angular 6 HttpClient return instance of class

断了今生、忘了曾经 提交于 2019-12-01 04:09:09
Before angular's new HttpClient was introduced, our objects returned from the http api call could be validated with the instanceof keyword. they no longer can with the HttpClient Module. I'm trying some simple methods but the type checks return false every time. the desired behavior of: ``` getCow() { return this.http.get<Cow>(ApiRoute.GET_COW, options) .map(res => res as Cow) .toPromise() .then((c: Cow) => { console.log(c instanceof Cow); //this is false }) } ``` would return true. does anyone know of a simple way to new up an instance behind the scenes of the http client? TypeScript uses

How to add multiple headers in Angular 5 HttpInterceptor

依然范特西╮ 提交于 2019-12-01 02:59:50
I'm trying to learn how to use HttpInterceptor to add a couple of headers to each HTTP request the app do to the API. I've got this interceptor: import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; @Injectable() export class fwcAPIInterceptor implements HttpInterceptor { intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const authReq = req.clone({ headers: req.headers.set('Content-Type', 'application/json') }); console.log(

How to add multiple headers in Angular 5 HttpInterceptor

无人久伴 提交于 2019-11-30 16:29:13
问题 I'm trying to learn how to use HttpInterceptor to add a couple of headers to each HTTP request the app do to the API. I've got this interceptor: import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; @Injectable() export class fwcAPIInterceptor implements HttpInterceptor { intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const

Angular 4 Error: No provider for HttpClient

二次信任 提交于 2019-11-30 06:04:43
I am starting a new angular project with the CLI and am running into a no provider for HttpClient error. I have added HttpClientModule to my module imports which seems to be the usual culprit in this scenario. Not finding a lot online other than that so I am not sure what the issue may be. my app.module.ts @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, HttpClientModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { } and my service @Injectable() export class FlexSearchService { constructor(private http: HttpClient) {} getSavedSearchResult(index:

How do I set the baseUrl for Angular HttpClient?

北城以北 提交于 2019-11-29 22:05:20
I did not find a way in the documentation to set the base API URL for HTTP requests. Is it possible to do this with the Angular HttpClient? TheUnreal Use the new HttpClient Interceptor. Create a proper injectable that implements HttpInterceptor : import {Injectable} from '@angular/core'; import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http'; import {Observable} from 'rxjs/Observable'; @Injectable() export class APIInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const apiReq = req