angular-httpclient

HttpClient not running constructor

 ̄綄美尐妖づ 提交于 2019-11-29 11:57:10
I'm fetching an object from a json endpoint using the HttpClient. After I fetch it and subscribe to the observable, I found that the constructor doesn't run on the model and the public methods on the object are all undefined. How do I get the constructor to run and the methods are available? export class Customer { constructor() { this.Addresses = new Array<Address>(); } public Addresses: Array<Address>; public addAddress(address: Address) void{ this.Addresses.push(address); } } var url: string = `${this.urlBase}api/customer/${id}`; var customerObservable: Observable<Customer> = this.authHttp

Use a promise in Angular HttpClient Interceptor

血红的双手。 提交于 2019-11-29 11:20:08
问题 Can I use promise within HttpInterceptor ? For example: export class AuthInterceptor implements HttpInterceptor{ this.someService.someFunction() .then((data)=>{ //do something with data and then return next.handle(req); }); } why I need this? because I need to get a token to add to request header before making the request to the server. My interceptor: @Injectable() export class AuthInterceptor implements HttpInterceptor{ constructor(private authService: AuthService){} intercept(req:

Angular HttpClient return expecting observable<HttpEvent<any> rather than observable<any>

天涯浪子 提交于 2019-11-28 23:28:33
I'm getting a compilation error on the return type when using HttpClient. In my function GetPortfolio , I'm expecting the GET call to return the json object of type Observable<Portfolio> but it's giving the error: Type Observable<HttpEvent<Portfolio>> is not assignable to type Observable<Portfolio> . Type HttpEvent<Portfolio> is not assignable to type Portfolio . Type HttpProgressEvent is not assignable to type Portfolio . Property name is missing in type HttpProgressEvent . My code: import { Injectable } from '@angular/core'; import { environment } from './environments/environment'; import {

Angular (5) httpclient observe and responseType: 'blob'

最后都变了- 提交于 2019-11-28 21:33:12
Context: I'm trying to download a binary file from a backend (that requires some data posted as json-body) and save it with file-saver using the filename specified by the backend in the content-disposition header. To access the headers I think I need the HttpResponse. But I'm unable to use angular's HttpClient.post<T>(...): Observable<HttpResponse<T>>; method with a Blob. When I call this.httpclient.post<Blob>('MyBackendUrl', params, {observe: 'response', responseType: 'blob'}); the compiler complains about the 'blob' ('json' is accepted by the compiler): error TS2345: Argument of type '{

Angular 6: HttpClient Get JSON File 404 Error

≯℡__Kan透↙ 提交于 2019-11-28 10:04:28
问题 Using the HTTP Client, I'm to retrieve a JSON file which resides the assets directory within my Angular 6 App which was generated using the CLI. While I know there are a couple related questions (and answers) related to this topic, but none have worked in my case. Below is my file structure: Specifically I'm trying to retrieve us-all-all.geo.json angular.json "projects": { "ng-ngrx-highcharts-example": { "root": "", "sourceRoot": "src", "projectType": "application", "prefix": "app",

Angular 5 caching http service api calls

半世苍凉 提交于 2019-11-28 09:17:58
In my Angular 5 app a certain dataset (not changing very often) is needed multiple times on different places in the app. After the API is called, the result is stored with the Observable do operator. This way I implemented caching of HTTP requests within my service. I'm using Angular 5.1.3 and RxJS 5.5.6. Is this a good practise? Are there better alternatives? import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/of'; import 'rxjs/add/operator/do'; @Injectable() export class

Angular (5) httpclient observe and responseType: 'blob'

守給你的承諾、 提交于 2019-11-27 14:48:08
问题 Context: I'm trying to download a binary file from a backend (that requires some data posted as json-body) and save it with file-saver using the filename specified by the backend in the content-disposition header. To access the headers I think I need the HttpResponse. But I'm unable to use angular's HttpClient.post<T>(...): Observable<HttpResponse<T>>; method with a Blob. When I call this.httpclient.post<Blob>('MyBackendUrl', params, {observe: 'response', responseType: 'blob'}); the compiler

Angular 4/5 HttpClient: Argument of type string is not assignable to 'body'

北战南征 提交于 2019-11-27 07:53:26
The Angular docs say: The response body doesn't return all the data you may need. Sometimes servers return special headers or status codes to indicate certain conditions, and inspecting those can be necessary. To do this, you can tell HttpClient you want the full response instead of just the body with the observe option: http .get<MyJsonData>('/data.json', {observe: 'response'}) .subscribe(resp => { // Here, resp is of type HttpResponse<MyJsonData>. // You can inspect its headers: console.log(resp.headers.get('X-Custom-Header')); // And access the body directly, which is typed as MyJsonData as

How to get body from HttpErrorResponse in Angular 6?

我只是一个虾纸丫 提交于 2019-11-26 22:07:51
问题 I have created a REST API call in my Angular app which downloads a file. I am setting responseType to 'blob' since I am expecting a file in response. But when there is no file available at the server the Response has a error code as 404 i.e Bad Request with some message in body. But I am not able to parse that error message from body since HttpErrorResponse is giving a blob object in error.error How do I get the actual body from the error object instead of blob. Also is there any way to

Catching errors in Angular HttpClient

独自空忆成欢 提交于 2019-11-26 21:31:21
I have a data service that looks like this: @Injectable() export class DataService { baseUrl = 'http://localhost' constructor( private httpClient: HttpClient) { } get(url, params): Promise<Object> { return this.sendRequest(this.baseUrl + url, 'get', null, params) .map((res) => { return res as Object }) .toPromise(); } post(url, body): Promise<Object> { return this.sendRequest(this.baseUrl + url, 'post', body) .map((res) => { return res as Object }) .toPromise(); } patch(url, body): Promise<Object> { return this.sendRequest(this.baseUrl + url, 'patch', body) .map((res) => { return res as Object