docs.rodeo

MDN Web Docs mirror

RequestInit

{{APIRef("Fetch API")}} 

The RequestInit dictionary of the Fetch API represents the set of options that can be used to configure a fetch request.

You can pass a RequestInit object into the {{domxref("Request.Request()", "Request()")}}  constructor, or directly into the fetch() function call.

You can also construct a Request with a RequestInit, and pass the Request to a fetch() call along with another RequestInit. If you do this, and the same option is set in both places, then the value passed directly into fetch() is used.

Instance properties

Examples

Passing options into fetch()

In this example we pass the method, body, and headers options directly into the fetch() method call:

async function post() {
  const response = await fetch("https://example.org/post", {
    method: "POST",
    body: JSON.stringify({ username: "example" }),
    headers: {
      "Content-Type": "application/json",
    },
  });

  console.log(response.status);
}

Passing options into the Request() constructor

In this example we create a {{domxref("Request")}} , passing the same set of options into its constructor, and then pass the request into fetch():

async function post() {
  const request = new Request("https://example.org/post", {
    method: "POST",
    body: JSON.stringify({ username: "example" }),
    headers: {
      "Content-Type": "application/json",
    },
  });

  const response = await fetch(request);

  console.log(response.status);
}

Passing options into both Request() and fetch()

In this example we create a {{domxref("Request")}} , passing the method, headers, and body options into its constructor. We then pass the request into fetch() along with body and referrer options:

async function post() {
  const request = new Request("https://example.org/post", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ username: "example1" }),
  });

  const response = await fetch(request, {
    body: JSON.stringify({ username: "example2" }),
    referrer: "",
  });

  console.log(response.status);
}

In this case the request will be sent with the following options:

Specifications

{{Specifications}} 

See also

In this article

View on MDN