Header customized angular 4, error does not authenticate

0

Good morning, I need to connect to a service that requires authentication by passing some headers, most of all ways I tried, does not send my custom header, here is the code below:

public listarCategorias(): Observable<category[]> {

    return this.http.get<category[]>('https://xxxxx/categories')
        .pipe(
        tap(heroes => { this.log('listarCategorias') }),
        catchError(this.handleError('listarCategorias', []))
        );
}

I made an Interceptor

 intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    const headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'x-accountmanager-key': 'xxxx',
        'x-api-key': 'xxxx',
        'x-user-email': 'xxxx',

    }

    const clone = req.clone({
        setHeaders: headers
    });

    return next.handle(clone);
}

I tested it in several applications like ARC, and authenticate it normally.

The Header I captured in the browser was as follows:

  

: method: OPTIONS: path: / categories: scheme: https accept: /   accept-encoding: gzip, deflate, br   accept-language: pt-BR, pt; q = 0.9, en-US; q = 0.8, en; q = 0.7   access-control-request-headers: content-type, x-accountmanager-key, x-api-key, x-user-email   access-control-request-method: GET origin: link   user-agent: Mozilla / 5.0 (Windows NT 10.0, Win64, x64)   AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 62.0.3202.94   Safari / 537.36

Arnaldo R. Portela, I changed his example, and it was not, when I run in ARC, it goes

    
asked by anonymous 02.12.2017 / 14:37

2 answers

0

Something similar happened in my project, but then I realized that it was the version of my dependencies, inside the root folder of your project runs

ng --version 

And the post appears.

    
03.12.2017 / 02:10
0

I've recreated your example in plnkr and it worked, check the link if my implementation is different than yours:

Example Link

Just remember that as you are using the HttpInterceptor, you should use the "HttpClient" class that is in "@hang / common / http" to perform the requests and not the "Http" class that is in "@hang / http ".

Edited: it sends two requests, one OPTIONS and then one GET, in the example I put, the headers are being changed and the return is OK. Of course I'm not putting a bearer and I'm not doing any authentication, but the header information is there.

    
02.12.2017 / 16:05