angular-httpclient

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

拜拜、爱过 提交于 2019-12-18 02:19:13
问题 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 {

Parse date with Angular 4.3 HttpClient

社会主义新天地 提交于 2019-12-17 22:46:17
问题 I'm currently switching to the new HttpClient of Angular 4.3. One advantage is that I can specify a type info on the GET method and that the returned JSON is parsed into the given type, e.g. this.http.get<Person> (url).subscribe(...) But unfortunately, all dates in the JSON are parsed as numbers in the resulting object (probably because the Java Date objects are serialized as numbers in the backend). With the old Http I used a reviver function when calling JSON.parse() like this: this.http

Angular 6 Get response headers with httpclient issue

喜欢而已 提交于 2019-12-17 04:35:12
问题 I'm using the code below to try to extract a value (ReturnStatus) from the response headers; Keep-Alive: timeout=5, max=100 ReturnStatus: OK,SO304545 Server: Apache/2.4.29 (Win32) OpenSSL/1.0.2m The code; import { Injectable } from '@angular/core'; import { Account } from './model/account'; import { HttpClient } from '@angular/common/http'; import { Observable, throwError } from "rxjs"; import { map, filter, catchError, mergeMap } from 'rxjs/operators'; createOrder(url) : Observable<any> {

How to add HttpClient Interceptors conditionally in Angular

做~自己de王妃 提交于 2019-12-14 04:16:10
问题 Recently I have been using Interceptors with Angular HttpClient. I add headers corresponding to some HTTP GET methods and for some I do not need those headers. How can I tell my interceptor to conditionally add interceptors to only those methods? I can even split up services like one service for headers and one without headers or one for different headers and one for different. NgModule providers { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true, },{ provide: HTTP

Recursively combining HTTP results based on response

倾然丶 夕夏残阳落幕 提交于 2019-12-14 03:59:41
问题 There's an API(https://panelapp.genomicsengland.co.uk/api/v1/panels/?page=1) that I want to consume all the data to my angular apps. The problem is that their API have pagination, and I want to retrieve all of the content at once. As you can see on the API, they actually have "next" attribute on their response which point to the next page. I want to be able to keep requesting from the API as long as "next" attribute is not null then combine all their response into one. I have tried using

Angular HttpClient: Convert HttpResponse to Response

亡梦爱人 提交于 2019-12-13 20:41:59
问题 While using Http we get response type as Response from @angular/http library, similarly converting to JSON using res.json() But by using HttpClient from @angular/common/http we get direct JSON or HttpResponse by using observe Is there any work around to convert type from Response to HttpResponse or any other way to get response from HtpClient so that .json() will work? 回答1: http client by default calls res.json() implicitly and you don't need to that manually yourself and set the respond type

edit and delete operation in angular

匆匆过客 提交于 2019-12-13 20:06:48
问题 I'm building a crud operation and was able to do the create operation. Now I'm trying to edit and delete them. I tried getting individual data through service but failed. It would be nice if anyone could help me with this. service.ts // Fetch Role Data getRoleData(){ var headers = new HttpHeaders().set('Authorization', 'Token ' + localStorage.getItem('usertoken')); var options_role = { headers: headers }; return this.httpClient.get('api/auth/role/', options_role); } // Add Data sendRole(data)

How to Show spinner for every HTTP requests in angular 5?

狂风中的少年 提交于 2019-12-13 13:06:15
问题 i am new to angular 5 . How to code a common function to show spinner for every HTTP request in angular 5.Please help me to implement this. 回答1: You can use Angular HttpInterceptor to show a spinner for all your requests, Here's a good medium article on how to implement an http interceptor Also, you will have to create a spinner service/module and inject it in your http interceptor. Finally, in your intercept method you can use the finally rxJs method to stop your spinner. Here's a simple

angular http post send complex objects

北慕城南 提交于 2019-12-13 12:41:05
问题 My Service public submitBooking(createBooking: CreateBooking) { const body = this.getSaveBookingRequestBody(createBooking); //const json = JSON.Stringify(body) //It throws 415 error return this.httpClient.post(this.baseUrl + 'Save', body ) .subscribe(); } private getSaveBookingRequestBody(createBooking: CreateBooking): any { const body = { 'Booking': { 'Header': this.getBookingHeader(createBooking), //It works fine. 'Items': [this.getBookingItems(createBooking)], }, 'TestOnly': !environment

How do I set the baseUrl for Angular HttpClient?

人盡茶涼 提交于 2019-12-12 07:08:51
问题 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? 回答1: 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 {