Request

최신 업데이트:2026-05-18 15:35:12

The Request interface represents an HTTP request and is a core component of the standard Fetch API. In Edge Cloud Apps, you can use the Request object to read information from incoming client requests or construct new requests to initiate subrequests.

Constructor

let request = new Request(input [, init])

Parameters

Parameter Type Required Description
input string | Request Yes The resource to fetch. This can be a string containing a URL or an existing Request object (in which case a copy is created).
init RequestInit No An optional configuration object containing custom settings to apply to the Request (see details below).

RequestInit Object Properties:

  • method (string): The HTTP request method, e.g., GET, POST. Defaults to GET.
  • headers (Headers): A Headers object or an object literal containing key-value pairs to set the request headers.
  • body (string | ReadableStream | FormData | URLSearchParams): The body of the request. Note that requests using the GET or HEAD methods cannot have a body.
  • redirect (string): The redirect mode to use: follow, error, or manual. When creating a new Request object via the constructor, the default is follow.

    Note: For the incoming request passed to the FetchEvent (i.e., event.request), the redirect mode defaults to manual.

Properties

Note: All properties of the incoming Request object (event.request) are read-only. If you need to modify the incoming request, create a new Request copy by passing the necessary options to the constructor: new Request(event.request, { ...modifications }).

  • body (ReadableStream, read-only)
    A stream of the body contents.
  • bodyUsed (Boolean, read-only)
    Indicates whether the request body has been read or used. Once read, this value becomes true.
  • headers (Headers, read-only)
    A Headers object containing the request’s headers.

    Important Note on Cookies: Unlike browser environments, edge functions place almost no restrictions on the headers you can send. The runtime does not manage cookies automatically; instead, it treats the Cookie header like any other request header, passing it through directly.

    Important Note on Redirect Headers: If a response is a redirect and the redirect mode is set to follow, all request headers (including sensitive headers like Cookie, Authorization, or custom headers) will be forwarded to the redirect target, even if the target is on a different domain. If this behavior is undesired, you should set the redirect mode to manual and implement a custom redirect handling strategy.

  • method (string, read-only)
    The HTTP request method, such as GET or POST.
  • redirect (string, read-only)
    The redirect mode for the request (follow, error, or manual). When set to follow, fetch() will automatically follow 3xx redirects. When set to manual, 3xx redirect responses will be returned directly to the caller.
  • url (string, read-only)
    The complete URL string of the request.

Instance Methods

The following methods are available on Request instances to read and process the body:

  • clone() : Promise<Request>
    Creates and returns an exact copy of the Request object.
  • arrayBuffer() : Promise<ArrayBuffer>
    Reads the request body and returns a Promise that resolves with an ArrayBuffer representation.
  • formData() : Promise<FormData>
    Reads the request body and returns a Promise that resolves with a FormData object (typically used for form submissions).
  • json() : Promise<Object>
    Reads the request body and returns a Promise that resolves with a JSON object.
  • text() : Promise<string>
    Reads the request body and returns a Promise that resolves with a UTF-8 string representation.

Notes

1. Asynchronous Tasks and Request Context

When you pass a Promise that resolves to a Response into the event.respondWith() method of a fetch event, all running asynchronous tasks can safely access the active request context until that Promise settles.

Typically, you pass the event to an asynchronous handler function as shown below:

addEventListener("fetch", event => {
  // Correct: Pass event.request to the async handler
  event.respondWith(requestHandler(event.request));
});

// Global scope: Request context is inaccessible here

async function requestHandler(request) {
  // The Request context is active and safe to use here
  console.log("Request URL: ", request.url);
  return new Response("Hello, Edge Cloud Apps!");
}

2. Errors Accessing Inactive Request Contexts

Attempting to call APIs like fetch() or access a specific Request context during the initial startup phase of the script (global scope) will throw an exception.

// INCORRECT: Initiating a fetch request in the global scope
const promise = fetch("https://example.com/"); 

addEventListener("fetch", event => {
  // ...
});

The snippet above will throw an error as soon as the script loads, preventing the fetch event listener from being registered. All fetch() calls and request processing must occur inside the event listener callbacks.

3. Content-Length Header Handling

When sending a request, the Content-Length header is automatically calculated and set by the runtime based on the data source of the Request. Any Content-Length value manually set in the Headers by your code will be ignored.

  • Fixed Length: If you need to specify a precise Content-Length, the body of the Request must be a data type with a known, fixed length (e.g., a FixedLengthStream, a string, or a TypedArray).
  • Chunked Transfer: When passing a general ReadableStream (with an unknown length) as the body, the runtime will automatically utilize chunked transfer encoding (Transfer-Encoding: chunked) for transmission.

References

이 문서의 내용이 도움이 되었습니까?
아니오
정상적으로 제출되었습니다.피드백을 주셔서 감사합니다.앞으로도 개선을 위해 노력하겠습니다.