填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

HttpRequest

一个外发的 HTTP 请求,带有一个可选的类型化的请求体(body)。

An outgoing HTTP request with an optional typed body.

查看"说明"...

      
      class HttpRequest<T> {
  constructor(method: string, url: string, third?: T | { headers?: HttpHeaders; context?: HttpContext; reportProgress?: boolean; params?: HttpParams; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; }, fourth?: { headers?: HttpHeaders; context?: HttpContext; reportProgress?: boolean; params?: HttpParams; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; })
  body: T | null
  headers: HttpHeaders
  context: HttpContext
  reportProgress: boolean
  withCredentials: boolean
  responseType: 'arraybuffer' | 'blob' | 'json' | 'text'
  method: string
  params: HttpParams
  urlWithParams: string
  url: string
  serializeBody(): ArrayBuffer | Blob | FormData | string | null
  detectContentTypeHeader(): string | null
  clone(update: { headers?: HttpHeaders; context?: HttpContext; reportProgress?: boolean; params?: HttpParams; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; body?: any; method?: string; url?: string; setHeaders?: { ...; }; setParams?: { ...; }; } = {}): HttpRequest<any>
}
    

说明

HttpRequest 表示一个外发请求,包括 URL、方法、请求头、请求体和其它请求配置项。 它的实例都是不可变的。要修改 HttpRequest,应该使用 clone 方法。

HttpRequest represents an outgoing request, including URL, method, headers, body, and other request configuration options. Instances should be assumed to be immutable. To modify a HttpRequest, the clone method should be used.

构造函数

3 个重载形式...

显示所有 隐藏所有 expand_more
Overload #1
      
      constructor(method: "DELETE" | "GET" | "HEAD" | "JSONP" | "OPTIONS", url: string, init?: { headers?: HttpHeaders; context?: HttpContext; reportProgress?: boolean; params?: HttpParams; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; })
    
参数
method "DELETE" | "GET" | "HEAD" | "JSONP" | "OPTIONS"
url string
init object
可选. 默认值是 `undefined`.

Overload #2
      
      constructor(method: "POST" | "PUT" | "PATCH", url: string, body: T, init?: { headers?: HttpHeaders; context?: HttpContext; reportProgress?: boolean; params?: HttpParams; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; })
    
参数
method "POST" | "PUT" | "PATCH"
url string
body T
init object
可选. 默认值是 `undefined`.

Overload #3
      
      constructor(method: string, url: string, body: T, init?: { headers?: HttpHeaders; context?: HttpContext; reportProgress?: boolean; params?: HttpParams; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; })
    
参数
method string
url string
body T
init object
可选. 默认值是 `undefined`.

属性

属性说明
body: T | null只读

请求体,如果没有则为 null

The request body, or null if one isn't set.

请求体无法确保自己是不可变的,因为它们可以包含指向任何自定义数据类型的引用。 不过,在拦截器中,要小心维护其幂等性 —— 把它们当做不可变对象。

Bodies are not enforced to be immutable, as they can include a reference to any user-defined data type. However, interceptors should take care to preserve idempotence by treating them as such.

headers: HttpHeaders只读

本请求的外发请求头。

Outgoing headers for this request.

context: HttpContext只读

Shared and mutable context that can be used by interceptors

reportProgress: boolean只读

该请求是否应该暴露出进度事件。

Whether this request should be made in a way that exposes progress events.

进度事件很昂贵(在每个事件中都会执行一次变更检测),所以只有当消费者关心这些事件时才应该请求这些进度事件。

Progress events are expensive (change detection runs on each event) and so they should only be requested if the consumer intends to monitor them.

withCredentials: boolean只读

此请求是否应该带着凭证(Cookie)一起外发。

Whether this request should be sent with outgoing credentials (cookies).

responseType: 'arraybuffer' | 'blob' | 'json' | 'text'只读

所期待的服务器响应类型。

The expected response type of the server.

它用来在把响应对象返回给被请求者之前以恰当的方式解析它。

This is used to parse the response appropriately before returning it to the requestee.

method: string只读

外发 HTTP 请求的方法。

The outgoing HTTP request method.

params: HttpParams只读

外发的 URL 参数。

Outgoing URL parameters.

To pass a string representation of HTTP parameters in the URL-query-string format, the HttpParamsOptions' fromString may be used. For example:

      
      new HttpParams({fromString: 'angular=awesome'})
    
urlWithParams: string只读

外发的 URL,及其所有 URL 参数。

The outgoing URL with all URL parameters set.

url: string只读 声明在构造函数中

方法

把无格式的请求体转换成适合传给服务器的序列化格式。

Transform the free-form body into a serialized format suitable for transmission to the server.

      
      serializeBody(): ArrayBuffer | Blob | FormData | string | null
    
参数

没有参数。

返回值

ArrayBuffer | Blob | FormData | string | null

检测请求体,并尝试给它推断出一个合适的 MIME 类型。

Examine the body and attempt to infer an appropriate MIME type for it.

      
      detectContentTypeHeader(): string | null
    
参数

没有参数。

返回值

string | null

如果没有合适的 MIME 类型,该方法就会返回 null

If no such type can be inferred, this method will return null.

3 个重载形式...

显示所有 隐藏所有 expand_more
Overload #1
      
      clone(): HttpRequest<T>
    
参数

没有参数。

返回值

HttpRequest<T>


Overload #2
      
      clone(update: { headers?: HttpHeaders; context?: HttpContext; reportProgress?: boolean; params?: HttpParams; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; body?: T; method?: string; url?: string; setHeaders?: { ...; }; setParams?: { ...; }; }): HttpRequest<T>
    
参数
update object
返回值

HttpRequest<T>


Overload #3
      
      clone<V>(update: { headers?: HttpHeaders; context?: HttpContext; reportProgress?: boolean; params?: HttpParams; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; body?: V; method?: string; url?: string; setHeaders?: { ...; }; setParams?: { ...; }; }): HttpRequest<V>
    
参数
update object
返回值

HttpRequest<V>