Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完成 Request 章节翻译 #9

Merged
merged 5 commits into from
May 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 57 additions & 62 deletions class-Request.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,51 @@

### class: Request

Whenever the page sends a request, the following events are emitted by puppeteer's page:
- ['request'](#event-request) emitted when the request is issued by the page.
- ['response'](#event-response) emitted when/if the response is received for the request.
- ['requestfinished'](#event-requestfinished) emitted when the response body is downloaded and the request is complete.
每当页面发送一个请求, 以下事件会被 puppeteer 页面触发
- ['request'](#event-request) 当请求发起后页面会触发这个事件.
- ['response'](#event-response) 请求收到响应的时候触发.
- ['requestfinished'](#event-requestfinished) 请求完成并且响应体下载完成时触发

If request fails at some point, then instead of 'requestfinished' event (and possibly instead of 'response' event), the ['requestfailed'](#event-requestfailed) event is emitted.

If request gets a 'redirect' response, the request is successfully finished with the 'requestfinished' event, and a new request is issued to a redirected url.
如果某些时候请求失败, 后续不会触发 'requestfinished' 事件(可能也不会触发 'response' 事件), 而是触发 ['requestfailed'](#event-requestfailed)事件

如果请求得到一个重定向的响应, 请求会成功地触发 'requestfinished' 事件, 并且对重定向的`url`发起一个新的请求

#### request.abort([errorCode])
- `errorCode` <[string]> Optional error code. Defaults to `failed`, could be
one of the following:
- `aborted` - An operation was aborted (due to user action)
- `accessdenied` - Permission to access a resource, other than the network, was denied
- `addressunreachable` - The IP address is unreachable. This usually means
that there is no route to the specified host or network.
- `connectionaborted` - A connection timed out as a result of not receiving an ACK for data sent.
- `connectionclosed` - A connection was closed (corresponding to a TCP FIN).
- `connectionfailed` - A connection attempt failed.
- `connectionrefused` - A connection attempt was refused.
- `connectionreset` - A connection was reset (corresponding to a TCP RST).
- `internetdisconnected` - The Internet connection has been lost.
- `namenotresolved` - The host name could not be resolved.
- `timedout` - An operation timed out.
- `failed` - A generic failure occurred.
- `errorCode` <[string]> 可选的错误码. 默认为`failed`, 可以是以下值:
- `aborted` - 操作被取消 (因为用户的行为)
- `accessdenied` - 访问资源权限不足(非网络原因)
- `addressunreachable` - 找不到IP地址 这通常意味着没有路由通向指定主机或者网络
- `connectionaborted` - 未收到数据发送的ACK信号导致的连接超时
- `connectionclosed` - 连接关闭(对应 TCP FIN 包)
- `connectionfailed` - 尝试连接失败.
- `connectionrefused` - 尝试连接拒绝.
- `connectionreset` - 连接被重置 (对应 TCP RST 包).
- `internetdisconnected` - 网络连接丢失.
- `namenotresolved` - 主机名字无法被解析.
- `timedout` - 操作超时.
- `failed` - 发生通用错误.
- returns: <[Promise]>

Aborts request. To use this, request interception should be enabled with `page.setRequestInterception`.
Exception is immediately thrown if the request interception is not enabled.
想要中断请求, 应该使用 `page.setRequestInterception` 来开启请求拦截, 如果请求拦截没有开启会立即抛出异常.

#### request.continue([overrides])
- `overrides` <[Object]> Optional request overwrites, which can be one of the following:
- `url` <[string]> If set, the request url will be changed
- `method` <[string]> If set changes the request method (e.g. `GET` or `POST`)
- `postData` <[string]> If set changes the post data of request
- `headers` <[Object]> If set changes the request HTTP headers
- `overrides` <[Object]> 可选的请求覆写选项, 可以是以下值中的一个:
- `url` <[string]> 如果设置的话, 请求url将会改变
- `method` <[string]> 如果设置的话, 会改变请求方法 (例如. `GET` 或者 `POST`)
- `postData` <[string]> 如果设置的话, 会改变请求要提交的数据
- `headers` <[Object]> 如果设置的话, 改变http请求头
- returns: <[Promise]>

Continues request with optional request overrides. To use this, request interception should be enabled with `page.setRequestInterception`.
Exception is immediately thrown if the request interception is not enabled.
想要用可选的请求覆写选项继续请求, 应该使用 `page.setRequestInterception` 来开启请求拦截, 如果请求拦截没有开启会立即抛出异常

#### request.failure()
- returns: <?[Object]> Object describing request failure, if any
- `errorText` <[string]> Human-readable error message, e.g. `'net::ERR_FAILED'`.
- returns: <?[Object]> 描述请求失败的对象, 如果有的话
- `errorText` <[string]> 人类可读的错误信息, 例如, `'net::ERR_FAILED'`.

The method returns `null` unless this request was failed, as reported by
`requestfailed` event.
`requestfailed` 事件触发后, 在没有请求失败的情况下, 这个方法会返回 `null`.

Example of logging all failed requests:
输出所有失败请求示例::

```js
page.on('requestfailed', request => {
Expand All @@ -71,29 +67,30 @@ page.on('requestfailed', request => {
```

#### request.frame()
- returns: <?[Frame]> A matching [Frame] object, or `null` if navigating to error pages.
- returns: <?[Frame]> 一个相应的 [Frame] 对象, 如果导航到错误页面的话, 则是'null'

#### request.headers()
- returns: <[Object]> An object with HTTP headers associated with the request. All header names are lower-case.
- returns: <[Object]> 该请求的 http 头对象. 所有头都采用小写的命名方式

#### request.method()
- returns: <[string]> Request's method (GET, POST, etc.)
- returns: <[string]> 请求方法 (GET, POST, .)

#### request.postData()
- returns: <[string]> Request's post body, if any.
- returns: <[string]> 请求提交的数据, 如果有的话.

#### request.redirectChain()
- returns: <[Array]<[Request]>>

A `redirectChain` is a chain of requests initiated to fetch a resource.
- If there are no redirects and the request was successful, the chain will be empty.
- If a server responds with at least a single redirect, then the chain will
contain all the requests that were redirected.
`redirectChain` 是一条获取资源的请求链

- 如果没有被重定向而且请求成功的话, 链将会被置空
- 如果服务器至少响应了一次重定向, 那么这条链将会包含所有重定向请求

`redirectChain` 会共享相同链上的所有请求.

`redirectChain` is shared between all the requests of the same chain.

For example, if the website `http://example.com` has a single redirect to
`https://example.com`, then the chain will contain one request:
举个例子, 如果网站 `http://example.com` 重定向一次到
`https://example.com`, 那么这条链就会包含一个请求:

```js
const response = await page.goto('http://example.com');
Expand All @@ -102,7 +99,7 @@ console.log(chain.length); // 1
console.log(chain[0].url()); // 'http://example.com'
```

If the website `https://google.com` has no redirects, then the chain will be empty:
如果网站 `https://google.com` 没有重定向, 那么链(数组)就会被置空:
```js
const response = await page.goto('https://google.com');
const chain = response.request().redirectChain();
Expand All @@ -112,22 +109,20 @@ console.log(chain.length); // 0
#### request.resourceType()
- returns: <[string]>

Contains the request's resource type as it was perceived by the rendering engine.
ResourceType will be one of the following: `document`, `stylesheet`, `image`, `media`, `font`, `script`, `texttrack`, `xhr`, `fetch`, `eventsource`, `websocket`, `manifest`, `other`.
包含渲染引擎识别出的请求资源类型
资源类型为以下值中的一个: `document`, `stylesheet`, `image`, `media`, `font`, `script`, `texttrack`, `xhr`, `fetch`, `eventsource`, `websocket`, `manifest`, `other`.

#### request.respond(response)
- `response` <[Object]> Response that will fulfill this request
- `status` <[number]> Response status code, defaults to `200`.
- `headers` <[Object]> Optional response headers
- `contentType` <[string]> If set, equals to setting `Content-Type` response header
- `body` <[Buffer]|[string]> Optional response body
- `response` <[Object]> 完成请求的响应对象
- `status` <[number]> 响应状态码, 默认为 `200`.
- `headers` <[Object]> 可选的响应头
- `contentType` <[string]> 设置的话, 等同于 `Content-Type` 响应头
- `body` <[Buffer]|[string]> 可选的响应体
- returns: <[Promise]>

Fulfills request with given response. To use this, request interception should
be enabled with `page.setRequestInterception`. Exception is thrown if
request interception is not enabled.
完成请求后会返回一个响应. 可以通过开启 `page.setRequestInterception` 选项来使用请求拦截, 如果请求拦截没有开启则会抛出异常.

An example of fulfilling all requests with 404 responses:
下面例子中, 所有执行完成的请求都会返回 404 响应体

```js
await page.setRequestInterception(true);
Expand All @@ -140,11 +135,11 @@ page.on('request', request => {
});
```

> **NOTE** Mocking responses for dataURL requests is not supported.
> Calling `request.respond` for a dataURL request is a noop.
> **注意** `request.respond` 不支持模拟响应 `dataURL`请求 .
> 对 `dataURL`请求使用 `request.respond`并不会起任何作用.

#### request.response()
- returns: <?[Response]> A matching [Response] object, or `null` if the response has not been received yet.
- returns: <?[Response]> 相应的 Response 对象, 如果没有收到响应则是`null`.

#### request.url()
- returns: <[string]> URL of the request.
- returns: <[string]> 请求的 URL .