Interface Response

interface Response {
    body: string;
    contentLength: number;
    headers: Record<string, string>;
    proto: string;
    request: Request;
    responseTimeMS: number;
    status: string;
    statusCode: number;
    json(): any;
}

Properties

body: string

body 代表服务器提供的响应。

contentLength: number

contentLength 记录关联内容的长度。 -1 表示长度未知。>= 0 表示可以从 body 中读取给定的字节数。

headers: Record<string, string>

headers 表示服务器响应的 HTTP 头。

proto: string

proto 表示协议。例如 'HTTP/1.0'。

request: Request

request 是为获得此响应而发送的请求。

responseTimeMS: number

responseTimeMS 表示请求响应时间,单位毫秒。

status: string

status 是来自服务器响应的 HTTP 状态消息。例如 '200 OK'。

statusCode: number

statusCode 是来自服务器响应的 HTTP 状态代码。例如 200。

Methods

  • 将 Response.body 反序列化为 json。

    import http from 'pts/http';

    export default function () {
    const data = {user_id: '12345'};
    const resp = http.get('http://mockhttpbin.pts.svc.cluster.local/get', {query: data});
    console.log(resp.json().args.user_id); // 12345
    };

    Returns any

    JSON对象