Request
The Request
↗ interface represents an HTTP request and is part of the Fetch API.
The most common way you will encounter a Request
object is as a property of an incoming request:
export default { async fetch(request, env, ctx) { return new Response('Hello World!'); },};
You may also want to construct a Request
yourself when you need to modify a request object, because the incoming request
parameter that you receive from the fetch()
handler is immutable.
export default { async fetch(request, env, ctx) { const url = "https://example.com"; const modifiedRequest = new Request(url, request); // ... },};
The fetch() handler
invokes the Request
constructor. The RequestInit
and RequestInitCfProperties
types defined below also describe the valid parameters that can be passed to the fetch() handler
.
let request = new Request(input, options)
-
input
string | Request- Either a string that contains a URL, or an existing
Request
object.
- Either a string that contains a URL, or an existing
-
options
options optional- Optional options object that contains settings to apply to the
Request
.
- Optional options object that contains settings to apply to the
An object containing properties that you want to apply to the request.
-
cache
undefined | 'no-store' | 'no-cache'
optional- Standard HTTP
cache
header. Onlycache: 'no-store'
andcache: 'no-cache'
are supported. Any other cache header will result in aTypeError
with the messageUnsupported cache mode: <attempted-cache-mode>
.
- Standard HTTP
-
cf
RequestInitCfProperties optional- Cloudflare-specific properties that can be set on the
Request
that control how Cloudflare’s global network handles the request.
- Cloudflare-specific properties that can be set on the
-
method
string optional- The HTTP request method. The default is
GET
. In Workers, all HTTP request methods ↗ are supported, except forCONNECT
↗.
- The HTTP request method. The default is
-
headers
Headers optional -
body
string | ReadableStream | FormData | URLSearchParams optional- The request body, if any.
- Note that a request using the GET or HEAD method cannot have a body.
-
redirect
string optional- The redirect mode to use:
follow
,error
, ormanual
. The default for a newRequest
object isfollow
. Note, however, that the incomingRequest
property of aFetchEvent
will have redirect modemanual
.
- The redirect mode to use:
-
signal
AbortSignal optional- If provided, the request can be canceled by triggering an abort on the corresponding
AbortController
.
- If provided, the request can be canceled by triggering an abort on the corresponding
An object containing Cloudflare-specific properties that can be set on the Request
object. For example:
// Disable ScrapeShield for this request.fetch(event.request, { cf: { scrapeShield: false } })
Invalid or incorrectly-named keys in the cf
object will be silently ignored. Consider using TypeScript and generating types by running wrangler types
to ensure proper use of the cf
object.
-
apps
boolean optional- Whether Cloudflare Apps ↗ should be enabled for this request. Defaults to
true
.
- Whether Cloudflare Apps ↗ should be enabled for this request. Defaults to
-
cacheEverything
boolean optional- Treats all content as static and caches all file types beyond the Cloudflare default cached content. Respects cache headers from the origin web server. This is equivalent to setting the Page Rule Cache Level (to Cache Everything). Defaults to
false
. This option applies toGET
andHEAD
request methods only.
- Treats all content as static and caches all file types beyond the Cloudflare default cached content. Respects cache headers from the origin web server. This is equivalent to setting the Page Rule Cache Level (to Cache Everything). Defaults to
-
cacheKey
string optional- A request’s cache key is what determines if two requests are the same for caching purposes. If a request has the same cache key as some previous request, then Cloudflare can serve the same cached response for both.
-
cacheTags
Array<string> optional- This option appends additional Cache-Tag headers to the response from the origin server. This allows for purges of cached content based on tags provided by the Worker, without modifications to the origin server. This is performed using the Purge by Tag feature.
-
cacheTtl
number optional- This option forces Cloudflare to cache the response for this request, regardless of what headers are seen on the response. This is equivalent to setting two Page Rules: Edge Cache TTL and Cache Level (to Cache Everything). The value must be zero or a positive number. A value of
0
indicates that the cache asset expires immediately. This option applies toGET
andHEAD
request methods only.
- This option forces Cloudflare to cache the response for this request, regardless of what headers are seen on the response. This is equivalent to setting two Page Rules: Edge Cache TTL and Cache Level (to Cache Everything). The value must be zero or a positive number. A value of
-
cacheTtlByStatus
{ [key: string]: number }
optional- This option is a version of the
cacheTtl
feature which chooses a TTL based on the response’s status code. If the response to this request has a status code that matches, Cloudflare will cache for the instructed time and override cache instructives sent by the origin. For example:{ "200-299": 86400, "404": 1, "500-599": 0 }
. The value can be any integer, including zero and negative integers. A value of0
indicates that the cache asset expires immediately. Any negative value instructs Cloudflare not to cache at all. This option applies toGET
andHEAD
request methods only.
- This option is a version of the
-
image
Object | null optional- Enables Image Resizing for this request. The possible values are described in