I found this issue in multiple places, but no clear solution. Just one get request is not working (401 Unauthorized), but when I look at debugger the code works fine, and all other requests are working fine. Chrome and Firefox work fine as well, no issues.
This is my interceptor service:
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(public auth: LoginService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Get the auth header from the service.
let authHeader = this.auth.getToken();
if (!authHeader) {
authHeader = '';
}
let authReq: any;
// Clone the request to add the new header.
if (this.auth.isAuthenticated()) {
authHeader = authHeader.replace(/^"(.*)"$/, '$1'); // Remove quotes from token start/end.
authReq = request.clone({headers: request.headers.set('Authorization', 'Bearer ' + authHeader)});
} else {
authReq = request.clone({headers: request.headers.set('Token', authHeader)});
}
// Pass on the cloned request instead of the original request.
return next.handle(authReq);
}
}
Anyone has an advice?
You just need to add /
and the end of the api end point. For example this will not work:
http://www.example.com/api/skill
but this will work:
http://www.example.com/api/skill/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With