什么是 Response ? 以及 fetch 中的 Response 接口

2024-11-16 星期六

Response 表示 HTTP 请求的响应,包含状态码、头部信息和主体内容等信息。浏览器中的 Response 继承了 Body,因此可以通过 Body 对象的方法获取响应主体内容。

使用方式

fetch API 的返回值是一个 Promise,它的处理成功的结果是 Response 对象,失败的结果是 TypeError。

它的构造参数为:

  1. body:和 fetch API 的 body 参数一致,表示请求主体的数据,默认为 null。
  2. init:ResponseInit 类型的对象,其具有以下属性:
    • status:HTTP 状态码,默认为 200。
    • statusText:HTTP 状态文本,则默认为空。
    • headers:HTTP 头部信息,默认为空。
ts
const const response: Responseresponse = new var Response: new (body?: BodyInit | null, init?: ResponseInit) => Response
This Fetch API interface represents the response to a request. [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response)
Response
('Hello, world!', {
ResponseInit.status?: number | undefinedstatus: 200, ResponseInit.statusText?: string | undefinedstatusText: 'OK', ResponseInit.headers?: HeadersInit | undefinedheaders: { 'Content-Type': 'text/plain' } }) var console: Console
The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v20.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v20.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```
@see[source](https://github.com/nodejs/node/blob/v20.11.1/lib/console.js)
console
.Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)
Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v20.x/api/util.html#utilformatformat-args) for more information.
@sincev0.1.100
log
(await const response: Responseresponse.Body.text(): Promise<string>
[MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/text)
text
())

属性介绍

  • status:HTTP 状态码,具体细节可以查看HTTP 状态码
  • statusText:HTTP 状态文本。
  • headers:HTTP 头部信息,Request 和 Response 中的 Headers
  • bodyUsed:响应主体是否已使用,返回 true 表示已使用,返回 false 表示未使用。
  • body:响应主体内容。
  • ok:请求是否成功,如果状态码在 200-299 范围内则为 true,否则为 false。
  • redirected:响应结果是否为重定向的结果。
  • type:响应类型,默认为 “default”。
    • basic:默认值,表示同源响应,暴露除了“Set-Cookie”之外的所有标头。
    • cors:表示跨域响应,某些标头和主体可以被访问。
    • error:网络错误,例如超时、DNS 失败等。
    • opaque:表示跨域请求的响应,但目标服务器不支持跨域,或者请求被限制为 no-cors 模式。
    • opaqueredirect:表示为跨域的重定向响应,且请求使用了 manual 的 redirect 选项,即手动设置重定向。
  • url:最终经过重定向后的 URL。
~ cd ../

Comment / 留言区