Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One GET request not working in Safari ( Authorization issue) - Angular 6

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?

like image 781
Nemanja G Avatar asked Oct 28 '22 23:10

Nemanja G


1 Answers

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/
like image 122
Laxmikanta Nayak Avatar answered Nov 15 '22 12:11

Laxmikanta Nayak