Request Object
The request object represents the HTTP request. It contains properties that provide details and context about the request, as well as properties that allow for modifying the request.
To prevent a 500 error response, limit the body string length to 2000 characters. If called multiple times, the event handler uses the most recent response.
Properties
body
A stream used to read the contents of the request body. This is a read-only ReadableStream value.
This property is only available when using the responseProvider event.
cacheKey
Returns the methods that specify the HTTP response in cache for an HTTP request.
For more information see the CacheKey Object.
clientIp
The original client IP address that can either be IPv4 or IPv6. This is a read-only string value.
cpCode
A unique identifier in the form of an unsigned integer that is used for reporting site traffic usage data. This is a read-only value.
device
Returns an object that contains properties specifying the client device characteristics.
For more information see the Device Object.
host
The host header value of the incoming request from the client. This is a read-only string value.
method
The HTTP method of the incoming request. This is a read-only string value.
path
The URL path of the incoming request, including the filename and extension, but without a query string. This is a read-only string value.
If the incoming request does specify a path, the value is /.
query
The query string of the incoming request. This is a read-only string value.
If the incoming request does not specify a query, the value is an empty string.
scheme
The scheme of the incoming request (“http” or “https”). This is a read-only string value.
url
The relative path and query string of the incoming request. This is a read-only string value.
userLocation
Returns an object that contains properties specifying the geographic location.
For more information see the User Location Object.
Methods
The following methods are available for the EdgeWorkers Request Object.
respondWith()
Constructs a response for the given request, rather than fetching a response from cache or the origin.
Responses constructed with respondWith() can create a body with a maximum of 2048 characters.
👍 The
Connection,Keep-Alive,Proxy-Authenticate,Proxy-Authorization,TE,Trailers, andTransfer-Encodinghop-by-hop headers should not be set when creating a request.The
Host,Content-Length, andVaryheaders should not be set or copied from another request. If you opt to set them anyway, you need to make sure that the values are correct.
- An incorrect value in the
Hostheader can break your request.- An incorrect value in the
Content-Lengthheader will break the response. Make sure that the value reflects the actual length of the payload you’re passing.- An incorrect value in the
Varyheader can break cacheability. 📘respondWith()supports the GET, POST, DELETE, PUT, PATCH, and HEAD request methods.
Parameters
Review the table for information about the available parameters. This method can be modified during the onClientRequest, onClientResponse, onOrigin Request, and onOrigin Response events.
📘 By default, you cannot add a “Set-Cookie” header to the
request.respondWith()header. You need to enable it with the metadata header tag described on the cookies page. Other response headers are supported.
📘 If you apply EdgeWorkers and an Edge Redirect Cloudlet to the same end user request, the Cloudlet cannot set the Location header when the EdgeWorker runs a
respondWith()method.
addHeader()
Renames or adds values to a header. If the header already exists, then the value is appended. The new header value can be a single string or an array. This request header can only be modified during the onClientRequestand onOriginRequestevents.
Parameters
Review the table for information about the available parameters.
getHeader()
Returns an array of header values by header name. The header names are case insensitive. If the header doesn’t exist, a value of undefined is returned. This request header can only be modified during the onClientRequestand onOriginRequestevents.
Parameters
Review the table for information about the available parameters.
getHeaders()
Returns a JavaScript object that contains all HTTP request headers as properties.
The key for each property is the name of the HTTP header, normalized to lower-case. The value is an array of strings, containing one string for each HTTP header with the same name. The header properties are in the order that they were received by the Akamai edge servers. When you iterate across the object properties you will get the headers in the order that the browser sent them.
👍 Refer to the code sample for the Redirect Liquidator use case in the EdgeWorkers GitHub repo for more information about how to use and debug
getHeaders().
setHeader()
Sets header values and overrides any previous headers. This request header can only be modified during the onClientRequest and onOriginRequest events.
Parameters
Review the table for information about the available parameters.
📘 EdgeWorkers cannot manipulate Akamai headers added for internal use. These headers typically start with ‘X-Ak’ and ‘Akamai-’.
removeHeader()
Removes the named header. This request header can only be modified during the onClientRequest and onOriginRequest events. The header name is case insensitive.
Parameters
Review the table for information about the available parameters.
getVariable()
Gets the value of a Property Manager user-defined variable. Only variables that start with a PMUSER_ prefix are available. The name of the variable must be UPPERCASE.
Parameters
Review the table for information about the available parameters.
📘
PMUSER_variables with a security setting of sensitive are not available within an EdgeWorkers function usinggetVariable.
setVariable()
Sets the value of a Property Manager user-defined variable. Only variables that start with a PMUSER_ prefix are available. The name of the variable must be UPPERCASE.
The total size limit when creating Property Manager user-defined variables is 1024 characters. This limit includes the name and value of the variable and only applies to EdgeWorkers added or modified using the JavaScript API.
The 1024 character limit is a modification limit that only applies to setVariable, not getVariable. For example, if you use advanced metadata to create a PMUSER_ variable in a property with a value that exceeds 1024 characters, the EdgeWorkers function can still read the value.
If however, you then wanted to modify that variable, you could do so up to 1024 characters (including the name and value of the variable). This 1024 limit is cumulative for all setVariable calls in the execution of a given event. If you exceed the limit, setVariable will throw an exception.
Parameters
📘
PMUSER_variables with a security setting of sensitive are not available within an EdgeWorkers function usingsetVariable.By default, the security settings of the
PMUSER_variable are hidden. Hidden security settings are not available in theX-Akamai-Session-Inforesponse headers if requested. 📘PMUSER_variables do not need to be defined in Property Manager before usage in an EdgeWorker event handler. You can reference a variable set inonClientRequestinonClientResponsewithout defining it in Property Manager.To reference a variable in Property Manager and in an EdgeWorkers function you need to define the variable within Property Manager. You can’t use a variable in Property Manager unless it is defined.
The following example uses
PMUSER_TEST_VARIABLEin two event handlers without being defined in Property Manager.When using variables in this way, note that
onOriginRequestandonOriginResponsemay run on a different machine thanonClientRequestandonClientResponse. This means that they might not have access to the the variable defined withinonClientRequest.
Review the table for information about the available parameters.
route()
Routes the outbound origin request to a pre-defined origin server, a modified path, or a modified query string. The destination must be a JavaScript object containing at least one of the following optional properties; path, query, or origin. If the destination is not a JavaScript object an error is thrown. This method can only be modified during the onClientRequest event.
Parameters
📘 Edge defined redirects always take precedence over modify forward paths. Therefore, the Edge Redirector and Redirect behaviors take precedence over EdgeWorkers forward route modifications made using the
route(destination)function.
Review the table for information about the available parameters.
Review the table below for information about the optional properties.
📘 You may have origins that produce different content for the same URL. When you are changing this kind of origin you need to ensure that you use the origin server hostname in the cache key. For more information see the Cache Key Hostname documentation.
text()
Reads the body to completion and returns a promise that resolves to a string containing the full body. The request is decoded as UTF-8, using the replacement character on encoding errors.
The maximum request size is 16 KB. If the request exceeds this limit the promise is rejected.
This method can only be called during the responseProvider event.
📘 The request is buffered, not streamed.
json()
Reads the body to completion. Returns a promise that resolves to an Object that is a result of parsing the body JSON.
The maximum request size is 16 KB. If the request exceeds this limit the promise is rejected.
This method can only be called during the responseProvider event.
📘 The request is buffered, not streamed.
arrayBuffer()
Reads the body to completion. Returns a promise that resolves to an ArrayBuffer containing the bytes of the request.
The maximum request size is 16 KB. If the request exceeds this limit the promise is rejected.
This method can only be called during the responseProvider event.
The above example adds up the unsigned value of each byte and writes the total as part of the response.
📘 The request is buffered, not streamed.
wasTerminated()
Checks to see if the EdgeWorkers function called the respondWith() method. If so, the edge server responds to the request immediately after the event handler returns.
Returns true if respondWith() was called in the current event handler and false otherwise.