'unable to read incoming http request header in Angular Interceptor class at initial time for Single sign option
Angular app redirect from another application. so we were added headers for angular app to achieve single sign option. But unable to read incoming request headers on Interceptor class in angular app at initial hit.
Note: but same header value able to access if we were used MVC application. unable to read in Angular app only.
@Injectable()
export class HeaderInterceptor implements HttpInterceptor {
uid: any = "";
err!: Error;
constructor(private errorDialogService: ErrorDialogService,
private router: Router,
private route: ActivatedRoute,
private log: LoggingService,
private paydetservice: PayDetailsService) { }
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.uid = req.headers.get('userid');
}
}
Solution 1:[1]
so, what you are doing there is reading the request headers: headers that your angular app sends to the server. What you want to do is read the response headers: from the server to the angular app.
You can do it like this:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
tap((httpEvent: HttpEvent<any>) => {
if (httpEvent instanceof HttpResponse) {
console.log(httpEvent.headers);
}
})
);
}
but i am not sure if you really want to do this in the interceptor. Usually is simpler to do it where you perform the request with the HttpClient
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
