请求首部字段是从客户端往服务器端发送请求报文中所使用的字段,用于补充请求的附加字段、客户端信息、对响应内容相关的优先级等内容。
在开始先说一下在实际使用中的配置,然后具体详解每个字段。在实际应用中,如何配置请求首部字段呢?下面以angular8为例,给出案例代码:
header;
httpOptions2;
token;
region;
regionurl = Environment.application.serviceAPI + '/console/region/v1/regions';//查询可部署区域接口
refreshToken () {
this.token = this.cookiesService.getCookie ('inspur_token');
this.region = this.cookiesService.getCookie ('NetworkRegion');
this.httpOptions2 = {
headers: new HttpHeaders ({
'Content-Type': 'application/json',
'Authorization': 'bearer ' + this.token
})
};
}
getVpcs (currentPage, limit): Observable<any> {
this.refreshToken ();
return this.http.get (this.navHeaderService.getFullPath ('vpc', this.region) + '/v1/vpcs/list?pageNo=' + currentPage +
'&pageSize=' + limit, this.httpOptions2).pipe (
tap (response => response),
catchError (this.handleError ())
);
}
接下来详解每个http请求头字段
1. Accept
Accept首部字段告诉服务器,用户代理能够处理的媒体类型及媒体类型的相对优先级。 可使用 type/subtype 这种形式, 一次指定多种媒体类型。
下面列举媒体类型的例子:
* 文本文件
text/html, text/plain, text/css ...
application/xhtml+xml, application/xml ...
* 图片文件
image/jpeg, image/gif, image/png ...
*视频文件
video/mpeg,video/quiktime...
* 应用程序使用的二进制文件
application/octet-stream, application/zip...
若想要给显示的媒体类型增加优先级,则使 q= 来额外表示权重值, 用分号(;) 进行分隔。 权重值 q 的范围是 0~1(可精确到小数点后 3 位) , 且 1 为最大值。 不指定权重 q 值时, 默认权重为 q=1.0。
当服务器提供多种内容时, 将会首先返回权重值最高的媒体类型。
实际案例如下: