Request - Web APIs 编辑

The Request interface of the Fetch API represents a resource request.

You can create a new Request object using the Request() constructor, but you are more likely to encounter a Request object being returned as the result of another API operation, such as a service worker FetchEvent.request.

Constructor

Request()
Creates a new Request object.

Properties

Request.cache Read only
Contains the cache mode of the request (e.g., default, reload, no-cache).
Request.context Read only This deprecated API should no longer be used, but will probably still work.
Contains the context of the request (e.g., audio, image, iframe, etc.)
Request.credentials Read only
Contains the credentials of the request (e.g., omit, same-origin, include). The default is same-origin.
Request.destination Read only
Returns a string from the RequestDestination enum describing the request's destination. This is a string indicating the type of content being requested.
Request.headers Read only
Contains the associated Headers object of the request.
Request.integrity Read only
Contains the subresource integrity value of the request (e.g., sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=).
Request.method Read only
Contains the request's method (GET, POST, etc.)
Request.mode Read only
Contains the mode of the request (e.g., cors, no-cors, same-origin, navigate.)
Request.redirect Read only
Contains the mode for how redirects are handled. It may be one of follow, error, or manual.
Request.referrer Read only
Contains the referrer of the request (e.g., client).
Request.referrerPolicy Read only
Contains the referrer policy of the request (e.g., no-referrer).
Request.url Read only
Contains the URL of the request.

Request implements Body, so it also inherits the following properties:

body Read only
A simple getter used to expose a ReadableStream of the body contents.
bodyUsed Read only
Stores a Boolean that declares whether the body has been used in a response yet.

Methods

Request.clone()
Creates a copy of the current Request object.

Request implements Body, so it also has the following methods available to it:

Body.arrayBuffer()
Returns a promise that resolves with an ArrayBuffer representation of the request body.
Body.blob()
Returns a promise that resolves with a Blob representation of the request body.
Body.formData()
Returns a promise that resolves with a FormData representation of the request body.
Body.json()
Returns a promise that resolves with a JSON representation of the request body.
Body.text()
Returns a promise that resolves with an USVString (text) representation of the request body.

Note: The Body functions can be run only once; subsequent calls will resolve with empty strings/ArrayBuffers.

Examples

In the following snippet, we create a new request using the Request() constructor (for an image file in the same directory as the script), then return some property values of the request:

const request = new Request('https://www.mozilla.org/favicon.ico');

const url = request.url;
const method = request.method;
const credentials = request.credentials;

You could then fetch this request by passing the Request object in as a parameter to a WindowOrWorkerGlobalScope.fetch() call, for example:

fetch(request)
  .then(response => response.blob())
  .then(blob => {
    image.src = URL.createObjectURL(blob);
  });

In the following snippet, we create a new request using the Request() constructor with some initial data and body content for an api request which need a body payload:

const request = new Request('https://example.com', {method: 'POST', body: '{"foo": "bar"}'});

const url = request.url;
const method = request.method;
const credentials = request.credentials;
const bodyUsed = request.bodyUsed;

Note: The body type can only be a Blob, BufferSource, FormData, URLSearchParams, USVString or ReadableStream type, so for adding a JSON object to the payload you need to stringify that object.

You could then fetch this api request by passing the Request object in as a parameter to a WindowOrWorkerGlobalScope.fetch() call, for example and get the response:

fetch(request)
  .then(response => {
    if (response.status === 200) {
      return response.json();
    } else {
      throw new Error('Something went wrong on api server!');
    }
  })
  .then(response => {
    console.debug(response);
    // ...
  }).catch(error => {
    console.error(error);
  });

Specifications

SpecificationStatusComment
Fetch
The definition of 'Request' in that specification.
Living StandardInitial definition

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:92 次

字数:11226

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文