Request

Last update:2024-08-14 19:50:32

The Request object represents an HTTP request and contains information such as the request’s URL, method, headers, and body. You can use the Request object in your Edge Cloud Apps Functions to access and manipulate incoming HTTP requests.

Constructor

new Request(input, init?);

Parameters:

Parameter Name Type Required Description
input Request | string | URL Yes The resource to be fetched. This can be a Request object, a string, or a URL object. If a Request object is passed in, a copy of that object will be created.
init object No An object used to define the properties of the request, such as the method, headers, and body.

Properties

  • body (read-only)

    A ReadableStream object representing the request body data stream.

  • bodyUsed (read-only)

    A boolean value indicating whether the request body has been used.

  • cache (read-only)

    A string representing the cache mode of the request, such as default, no-cache, reload, force-cache, only-if-cached, etc.

  • cf (read-only)

    An object containing Cloudflare-specific properties. For example:

    • colo: A string corresponding to the colo code of the Cloudflare data center where the request is located.
  • credentials (read-only)

    A string representing the credentials mode of the request, such as include, same-origin, omit, etc.

  • destination (read-only)

    A string representing the destination of the request, such as document, style, script, image, font, etc.

  • headers (read-only)

    A Headers object containing the request header information.

  • integrity (read-only)

    A string representing the integrity metadata of the request, used to verify the integrity of the response.

  • isHistoryNavigation (read-only)

    A boolean value indicating whether the request is a history navigation.

  • isReloadNavigation (read-only)

    A boolean value indicating whether the request is a reload navigation.

  • keepalive (read-only)

    A boolean value indicating whether to keep the connection alive.

  • method (read-only)

    A string representing the request method, such as GET, POST, PUT, DELETE, etc.

  • mode (read-only)

    A string representing the mode of the request, such as cors, no-cors, same-origin, etc.

  • redirect (read-only)

    A string representing the redirect mode of the request, such as follow, error, manual, etc.

  • referrer (read-only)

    A string representing the referrer of the request.

  • referrerPolicy (read-only)

    A string representing the referrer policy of the request, such as no-referrer, no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url, etc.

  • signal (read-only)

    An AbortSignal object used to abort the request.

  • url (read-only)

    A string representing the URL of the request.

  • clientIP (read-only)

    A string representing the client’s IP address.

Methods

  • clone(): Creates a copy of the Request object.

  • arrayBuffer(): Returns a Promise that resolves with an ArrayBuffer containing the request body data.

  • blob(): Returns a Promise that resolves with a Blob containing the request body data.

  • formData(): Returns a Promise that resolves with a FormData object containing the request body data.

  • json(): Returns a Promise that resolves with a JSON object containing the request body data.

  • text(): Returns a Promise that resolves with a string containing the request body data.

Code Example

async function handleRequest(event) {
  // Get the Request object
  const request = event.request;

  // Print the request method, URL, and client IP address
  console.log(`Request Method: ${request.method}`);
  console.log(`Request URL: ${request.url}`);
  console.log(`Client IP: ${request.clientIP}`); // Access the new clientIP property

  // Create a copy of the request
  const newRequest = request.clone();

  // Get the request body data
  const requestBody = await request.text();

  // Return a response
  return new Response('Request processed', { status: 200 });
}

addEventListener('fetch', (event) => {
  event.respondWith(handleRequest(event));
});

References

Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!