2

In internet, I see only add parameter to HTTP request like below.

  this.headers.append('Content-Type', 'application/json');
  this.headers.append('Accept', 'application/json');
  this.headers.append('Access-Control-Allow-Credentials', 'true');

I cannot find how can I get HTTP request header params in Angular 4.

tanmay
  • 7,761
  • 2
  • 19
  • 38
stef
  • 91
  • 2
  • 10

2 Answers2

1

if this is of any use for someone looking for this answer nowadays:

request.headers.forEach(h=> {
console.log(h);
console.log(request.headers.get(h));
})

that will print in your console all the header names, with their corresponding values, whenever request is an HttpRequest.

I am using it inside my interceptor for implementing a V4 signature in my angular 10 app and it is working ATM.

You get HttpHeaders {normalizedNames: Map(7), lazyUpdate: null, lazyInit: null, headers: Map(7)} when looking into the request.headerscontent in developer tools because it is lazy loaded, however if you look into it again after running the code I pasted above, you should see in the headers property all your existing headers and their content (that should match what was printed in the console).

Source: I tried in the console in a running Angular 10 app, while paused in a breakpoint inside the intercept method of an Http Interceptor.

-1

The Headers class also has a get(name: string) method, which returns a string, as well as other helpful methods for inspecting the headers. From the Angular docs:

https://angular.io/api/common/http/HttpHeaders

class HttpHeaders {
  constructor(headers?: string | {...})
  has(name: string): boolean
  get(name: string): string | null
  keys(): string[]
  getAll(name: string): string[] | null
  append(name: string, value: string | string[]): HttpHeaders
  set(name: string, value: string | string[]): HttpHeaders
  delete(name: string, value?: string | string[]): HttpHeaders
}

You could also use your browser's developer tools to inspect the request/response headers.

EDIT: It might be helpful if you specify exactly what it is you need to do. Is there a particular header that you need to inspect?

EDIT2: There's also the deprecated Headers class. Not sure which you're using, but there's a lot of overlap in terms of the methods available so it may not even matter.

https://angular.io/api/http/Headers

class Headers {
  constructor(headers?: Headers | {...})
  static fromResponseHeaderString(headersString: string): Headers
  append(name: string, value: string): void
  delete(name: string): void
  forEach(fn: (values: string[], name: string | undefined, headers: Map<string, string[]>) => void): void
  get(name: string): string | null
  has(name: string): boolean
  keys(): string[]
  set(name: string, value: string | string[]): void
  values(): string[][]
  toJSON(): {...}
  getAll(name: string): string[] | null
  entries()
}
Chris Yeager
  • 134
  • 6