Last update:2024-08-14 19:50:31
The fetch()
method provides an interface for asynchronously fetching resources via HTTP requests within your Edge Cloud Apps Functions.
Note: Asynchronous tasks, such as fetch()
, must be executed within a handler. If you attempt to call fetch()
within the global scope, your function will throw an error.
fetch(resource, options); // options is optional
The fetch()
function returns a Promise
object that eventually resolves to a Response
object.
resource
: Request
| string
| URL
The resource to be fetched. This can be a Request
object, a string, or a URL
object.
options
(Optional): object
An object that defines the content and behavior of the request. Optional properties include:
method
: The request method, such as GET
, POST
, PUT
, DELETE
, etc.headers
: The request headers, an object containing key-value pairs of headers.body
: The request body, which can be a string, FormData
, Blob
, ArrayBuffer
, etc.mode
: The request mode, such as cors
, no-cors
, same-origin
, etc.credentials
: The credentials mode, such as include
, same-origin
, omit
, etc.cache
: The cache mode, such as default
, no-cache
, reload
, force-cache
, only-if-cached
, etc.redirect
: The redirect mode, such as follow
, error
, manual
, etc.referrer
: The referrer of the request, which can be a string or a URL
object.referrerPolicy
: 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.integrity
: The integrity metadata of the request, used to verify the integrity of the response.keepalive
: A boolean value indicating whether to keep the connection alive.signal
: An AbortSignal
object used to abort the request.ignoreHttpsCert
: A boolean value indicating whether to ignore HTTPS certificate verification errors. Defaults to false
.cdnCache
: An object to configure CDN caching behavior. Optional properties include:
ignoreCache
: A boolean value indicating whether to ignore CDN caching. Defaults to false
.maxAge
: A number representing the maximum age (in seconds) for the cached object to be considered valid.sMaxAge
: A number representing the maximum age (in seconds) for the shared cached object to be considered valid. This only applies to the public
cache.The following code snippet shows how to make a POST
request using the fetch()
method, and how to set options for ignoring HTTPS certificate errors and CDN caching behavior:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const init = {
method: 'POST',
headers: {
'content-type': 'application/json;charset=UTF-8',
'custom-header1': 'value1',
'custom-header2': 'value2'
},
body: JSON.stringify({
key1: 'value3',
key2: 'value4'
}),
// Ignore HTTPS certificate errors
ignoreHttpsCert: true,
// Configure CDN caching behavior
cdnCache: {
ignoreCache: false,
maxAge: 3600,
sMaxAge: 1800
}
}
// Make the request
const response = await fetch('http://example.com', init)
// Return the response to the client
return response
}