angular-httpclient

Angular httpClient interceptor error handling

*爱你&永不变心* 提交于 2019-12-04 06:16:17
after reading the documentation on angular about http client error handling, I still don't understand why I don't catch a 401 error from the server with the code below: export class interceptor implements HttpInterceptor { intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { console.log('this log is printed on the console!'); return next.handle(request).do(() => (err: any) => { console.log('this log isn't'); if (err instanceof HttpErrorResponse) { if (err.status === 401) { console.log('nor this one!'); } } }); } } on the console log, I also get this: zone.js

Getting response headers from HttpClient post request in Angular?

不想你离开。 提交于 2019-12-04 04:19:54
问题 I'm trying to get the response headers from a post request, but the HttpResponse object doesn't contain the same headers that I can see in the network. What am I doing wrong? I need to access the value of the Apiproxy-Session-Id key and it isn't present in the HttpHeaders. This is my code to execute the post request and log the full response, where http is an HttpClient object. this.http.post('http://localhost:8081/user/login', JSON.stringify(requestBody), {observe: 'response'}).subscribe

How to catch error in Observable.forkJoin(…)?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 23:21:09
I use Observable.forkJoin() to handle the response after both http calls finishes, but if either one of them returns an error, how can I catch that error? Observable.forkJoin( this.http.post<any[]>(URL, jsonBody1, postJson) .map((res) => res), this.http.post<any[]>(URL, jsonBody2, postJson) .map((res) => res) ) .subscribe(res => this.handleResponse(res)) siva636 You may catch the error in each of your observables that are being passed to forkJoin : // Imports that support chaining of operators in older versions of RxJS import {Observable} from 'rxjs/Observable'; import {forkJoin} from 'rxjs

Angular - HTTP interceptor to retry requests with specific error status?

半腔热情 提交于 2019-12-03 21:09:34
I am trying to use an interceptor to handle http errors and retry for a special error status, in my case the status code 502. intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(request) .pipe( retryWhen(errors => { return errors .pipe( mergeMap(error => (error.status === 502) ? throwError(error) : of(error)), take(2) ) }) ) } But it's not working, whereas when I am using retry() in this fashion, it's working perfectly intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(request) .pipe(

Angular 6 Boilerplate with only Basic Features (Required all the time)?

不想你离开。 提交于 2019-12-03 21:00:56
I Setup Angular all the time with new Project. Anyone can suggest good boilerplate where no need to develop Basic required features which are required most of the times in a application. JWT Authentication in Angular 6 Bootstrap/Material Design setup in Angular 6 Http Services in Angular 6 File Uploading in Angular 6 Basic CRUD in Angular 6 Please suggest. It will save my lot of time. You can follow this boilerplate https://github.com/brnrajoriya/Angular-Ready-To-Use-Boilerplate Just clone this project and run npm install and you got all necessary components and a CRUD with JWT authentication

Display Subscribe Data in Angular 4

烈酒焚心 提交于 2019-12-03 20:28:13
I need help in displaying the output of subscribe from an api in Angular 4. How can i do this since I wrote data.data.data but it says property data doesnt exist on type object. How would i output it in the browser? Here's my code below and the api picture below import { Component, OnInit } from '@angular/core'; import { NewsService } from '../news.service'; @Component({ selector: 'app-news-list', templateUrl: './news-list.component.html', styleUrls: ['./news-list.component.css'] }) export class NewsListComponent implements OnInit { constructor(private newsService: NewsService) { } ngOnInit()

Angular - HttpClient: Map Get method object result to array property

二次信任 提交于 2019-12-03 11:55:56
问题 I am calling an API that returns a JSON Object. I need just the value of the array to map to a Observable . If I call api that just returns the array my service call works. Below is my sample code .. // my service call .. import { Injectable } from '@angular/core'; import {Observable} from 'rxjs/Observable'; import {Show} from '../models/show'; import {HttpClient} from '@angular/common/http'; @Injectable() export class MyService { constructor(private http: HttpClient ) { } findAllShows():

How to add headers to my Angular post request?

老子叫甜甜 提交于 2019-12-03 11:08:54
For a school project I need to make a simple login page with Angular. When a login button is clicked I need to add an Authorization header with my post. I created a backend and when I post my Authorization value to that backend with postman it works so nothing wrong with the backend. When I try to post to the same backend with my frontend it doesn't work. What is the best way to add headers to your post? It seems that the opinions are divided. This is my code: export class LoginComponent{ title = 'Login'; email = ''; password = ''; credentials = ''; basic = ''; constructor(private http

Multiple Objects into HttpParams

这一生的挚爱 提交于 2019-12-03 08:50:00
I have some categories in a formcontrol, I send them in an array of string like this: [1,4,6] And that is my actual code: let categoryIds = new Array<String>() this.selectedCategories.forEach((value: string, key: string) => categoryIds.push(key)) let requestOptions = { params: new HttpParams() .set('title', this.createNewForm.controls['title'].value) .append('content', this.createNewForm.controls['content'].value) .append('categoryids', categoryIds.toString()), withCredentials: true } But I want to send them as an array of objects, with the old version of angular Http I was able to do a

How to mock Angular 4.3 httpClient an error response in testing

ⅰ亾dé卋堺 提交于 2019-12-03 08:27:59
问题 I have a below interceptor auth-interceptor.service.ts import {Injectable, Injector} from '@angular/core'; import {HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http'; import {Observable} from 'rxjs/Observable'; import {Cookie} from './cookie.service'; import {Router} from '@angular/router'; import {UserService} from './user.service'; import {ToasterService} from '../toaster/toaster.service'; import 'rxjs/add/operator/catch'; import 'rxjs/add