如何在Angular 4中获取HTTP请求标头? [英] How can I get HTTP request headers in Angular 4?

查看:125
本文介绍了如何在Angular 4中获取HTTP请求标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在互联网上,我仅看到将参数添加到HTTP请求中,如下所示.

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');

我找不到如何在Angular 4中获取HTTP请求标头参数.

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

推荐答案

Headers类还具有get(name:string)方法,该方法返回字符串,以及用于检查标题的其他有用方法.来自Angular文档:

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.

如果您确切指定需要执行的操作,则可能会有所帮助.是否需要检查特定的标头?

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?

还有不推荐使用的Headers类.不确定您使用的是哪种,但是在可用方法方面有很多重叠之处,因此甚至可能无关紧要.

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()
}

这篇关于如何在Angular 4中获取HTTP请求标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆