Override request-id http header produced by HttpClient in Angular2

Viewed 468

I have a simple method that sends an HTTP POST request to my backend API with a use of the HttpClient. The code snippet:

import { HttpClient } from "@angular/common/http";
//other imports...

export class ApiService {
  private readonly apiUrl: Url;
  private readonly bearerToken: string;
  //other fields...

  public constructor(
    initialData: InitialDataService,
    private readonly httpClient: HttpClient
  ) {
    //some logic here
  }
    public sendCommand(command: ICommand): Observable<void> {
        const { __name, ...data } = command;
    
        return this.httpClient.post<void>(this.createCommandUrl(__name), data, {
          headers: {
            authorization: this.bearerToken,
            "request-id": createGuid()
          }
        });
      }

//...
}

When I run the code in the localhost Im able to see the generated Guid in the browser. enter image description here

When I deploy the changes to Azure App service and I open the developer tools in the browser(tested in Firefox and Chrome), I'm able to see that the request-id is defined as an array of values rather than a single value produced by my method createGuid():

enter image description here

There is some additional value-added. Not sure why it behaves differently in the Cloud...

Is there a way to force the browser to use only the request-id provided by me and remove that extra one that appeared? I'd like to be able to do this for all types of HTTP calls. Cheers

1 Answers

Are you using something like applicationinsights-web in the front end? If so then this has a feature where it will add request-id and request-context headers to all XHR requests, unless you explicitly disable it. It might not be the Angular HTTP Client at all.

There are 4 ways of configuring this.

  1. set disableCorrelationHeaders to true to switch it off for all requests
  2. set correlationHeaderExcludedDomains to switch it off for requests to specific domains
  3. set correlationHeaderExcludePatterns to fine tune which requests include\exlude the headers
  4. set enableCorsCorrelation to false to switch it off for all CORS requests

Additional documentation is available here: https://github.com/microsoft/ApplicationInsights-JS

And I believe the implementation of the code that checks these config values is here: https://github.com/microsoft/ApplicationInsights-JS/blob/05a1f410589514a35e70d425b037888bb3d17d9f/shared/AppInsightsCommon/src/Util.ts#L735

Related