Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

363
Visualizações
Disable Angular HttpInterceptor for some call

I have an angular application with an HttpInterceptor that catch the http errors to show some dialog, common to all my application.

I would like to disable the interceptor for some specific calls but I prefer to disable the default behaviour where i call the http, instead of write an exception into the interceptor. Anyone have found this problem?

I can be more specific with an example, if needed.

Regards

Davide

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can use HttpBackend to do this.

Description: When injected, HttpBackend dispatches requests directly to the backend, without going through the interceptor chain.

Using: You can use same like HttpClientby import it from @angular/common/http

Example:

import { HttpClient, HttpBackend } from '@angular/common/http';

...

@Injectable({
  providedIn: 'root'
})
export class HttpHelperService {

  private httpClient: HttpClient;

  constructor( httpBackend: HttpBackend) { 
     this.httpClient = new HttpClient(httpBackend);
  }

  // use like normal with HttpClient. However, should name it carefully to separate which http request go throught interceptor and which is not
  put(path: string, body: Object = {}): Observable<any> {
    return this.httpClient.put(
      `${this.URL}${path}`,
      JSON.stringify(body)
    ).pipe(catchError(this.formatErrors));
  }

....

ref: https://angular.io/api/common/http/HttpBackend

over 4 years ago · Santiago Trujillo Relatório

0

With Angular 12, it's possible now to include some metadata (with HttpContext) in your call that can be used within the interceptor to make decisions (or anything you want really).

Example

Your interceptor:

export const BYPASS_LOG = new HttpContextToken(() => false);

export class MyLogInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (req.context.get(BYPASS_LOG) === true)
      return next.handle(req);

    console.log(`req to ${req.url}`);

    return next.handle(req);
  }
}

Your service:

httpClient.get('https://example.com/', { context: new HttpContext().set(BYPASS_LOG, true) });

You can checkout angular docs for more information:

  • Intercepting requests and responses
  • Passing metadata to interceptors
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda