HttpRequest

An outgoing HTTP request with an optional typed body.

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

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

说明

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.

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

构造函数

3 个重载形式...

constructor(method: 'DELETE' | 'GET' | 'HEAD' | 'JSONP' | 'OPTIONS', url: string, init?: { headers?: HttpHeaders; reportProgress?: boolean; params?: HttpParams; responseType?: 'arraybuffer' | 'blob' | 'json' | 'text'; withCredentials?: boolean; })

参数

method

Type: 'DELETE' | 'GET' | 'HEAD' | 'JSONP' | 'OPTIONS'.

url

Type: string.

init

Type: { headers?: HttpHeaders; reportProgress?: boolean; params?: HttpParams; responseType?: 'arraybuffer' | 'blob' | 'json' | 'text'; withCredentials?: boolean; }.

可选. 默认值是 undefined.


constructor(method: 'POST' | 'PUT' | 'PATCH', url: string, body: T | null, init?: { headers?: HttpHeaders; reportProgress?: boolean; params?: HttpParams; responseType?: 'arraybuffer' | 'blob' | 'json' | 'text'; withCredentials?: boolean; })

参数

method

Type: 'POST' | 'PUT' | 'PATCH'.

url

Type: string.

body

Type: T | null.

init

Type: { headers?: HttpHeaders; reportProgress?: boolean; params?: HttpParams; responseType?: 'arraybuffer' | 'blob' | 'json' | 'text'; withCredentials?: boolean; }.

可选. 默认值是 undefined.


constructor(method: string, url: string, body: T | null, init?: { headers?: HttpHeaders; reportProgress?: boolean; params?: HttpParams; responseType?: 'arraybuffer' | 'blob' | 'json' | 'text'; withCredentials?: boolean; })

参数

method

Type: string.

url

Type: string.

body

Type: T | null.

init

Type: { headers?: HttpHeaders; reportProgress?: boolean; params?: HttpParams; responseType?: 'arraybuffer' | 'blob' | 'json' | 'text'; withCredentials?: boolean; }.

可选. 默认值是 undefined.

属性

属性名类型说明
body只读

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

请求体,如果没有则为 null

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只读

Outgoing headers for this request.

本请求的外发请求头。

reportProgress只读

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只读

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

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

responseType只读

The expected response type of the server.

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

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

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

method只读

The outgoing HTTP request method.

外发 HTTP 请求的方法。

params只读

Outgoing URL parameters.

外发的 URL 参数。

urlWithParams只读

The outgoing URL with all URL parameters set.

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

url只读 声明于构造函数中

方法

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

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

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

detectContentTypeHeader(): string | null

参数

没有参数。

返回值

string | null

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

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

3 个重载形式...

clone(): HttpRequest<T>

参数

没有参数。

返回值

HttpRequest<T>


clone(update: { headers?: HttpHeaders; reportProgress?: boolean; params?: HttpParams; responseType?: 'arraybuffer' | 'blob' | 'json' | 'text'; withCredentials?: boolean; body?: T | null; method?: string; url?: string; setHeaders?: { [name: string]: string | string[]; }; setParams?: { [param: string]: string; }; }): HttpRequest<T>

参数

update

Type: { headers?: HttpHeaders; reportProgress?: boolean; params?: HttpParams; responseType?: 'arraybuffer' | 'blob' | 'json' | 'text'; withCredentials?: boolean; body?: T | null; method?: string; url?: string; setHeaders?: { [name: string]: string | string[]; }; setParams?: { [param: string]: string; }; }.

返回值

HttpRequest<T>


clone<V>(update: { headers?: HttpHeaders; reportProgress?: boolean; params?: HttpParams; responseType?: 'arraybuffer' | 'blob' | 'json' | 'text'; withCredentials?: boolean; body?: V | null; method?: string; url?: string; setHeaders?: { [name: string]: string | string[]; }; setParams?: { [param: string]: string; }; }): HttpRequest<V>

参数

update

Type: { headers?: HttpHeaders; reportProgress?: boolean; params?: HttpParams; responseType?: 'arraybuffer' | 'blob' | 'json' | 'text'; withCredentials?: boolean; body?: V | null; method?: string; url?: string; setHeaders?: { [name: string]: string | string[]; }; setParams?: { [param: string]: string; }; }.

返回值

HttpRequest<V>