Methods for the Request Object

The following methods are available for the EdgeWorkers Request objects.

respondWith(status, headers, body,[deny_reason])

Constructs a response for the given request, rather than fetching a response from cache or the origin.

Review the table for information about the possible arguments.
Argument Description Object Type
status HTTP status code Integer
headers Properties used as key:value pairs for the response headers Object
body Content of the response body String
deny_reason [optional] Deny reason when the status code is a 403 String
Note: This method is currently limited to constructing responses for GET requests.
Note: You cannot add a“SetCookie” object to the request.respondWith() header. Other response headers are supported.
Note: 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.
// generate an empty API response
request.respondWith(200, {'Content-Type': ['application/json;charset=utf-8']  }, '{}');
// => {}
// generate a denied API response
request.respondWith(403, {'Content-Type': ['application/json;charset=utf-8']  }, '{}', 'Denied Response');
// => {} 

getHeader(name)

Returns an array of header values by header name. The header names are case insensitive. If the header doesn’t exist, an undefined array is returned. Request headers can only be modified during onClientRequest and onOriginRequest events.

Review the table for information about the possible arguments.

Argument Description Object Type
name Name of the header Array
// User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:66.0) Gecko/20100101 Firefox/66.0
request.getHeader('User-Agent')[0];
// => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:66.0) Gecko/20100101 Firefox/66.0"

setHeader(name, value)

Sets header values and overrides any previous headers. Request headers can only be modified during the onClientRequest and onOriginRequest events.

Review the table for information about the possible arguments.

Argument Description Object Type
name Name of the header String
value Value of the header String
Note: EdgeWorkers cannot manipulate Akamai headers added for internal use. These headers typically start with ‘X-Ak’ and ‘Akamai-’.
// Referer: http://www.example.com
request.setHeader('Referer', 'http://www.akamai.com');
// Referer: http://www.akamai.com

addHeader(name, value)

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. Request headers can only be modified during the onClientRequest and onOriginRequest events.

Review the table for information about the possible arguments.

Argument Description Object Type
name New header name String
value Value of the header String
//
request.addHeader('HeaderName', 'HeaderValue');
// HeaderName: HeaderValue

removeHeader(name)

Removes the named header. Request headers can only be modified during onClientRequest and onOriginRequest events. The header name is case insensitive.

Review the table for information about the possible arguments.

Argument Description Object Type
name Name of the header to remove String
// Pragma: HeaderName
request.removeHeader('Pragma');
//

getVariable(name)

Gets the value of a Property Manager user-defined variable. Only variables that start with a PMUSER prefix are available.

Review the table for information about the possible arguments.

Argument Description Object Type
name Names of the user-defined variable String
request.getVariable('PMUSER_MYVARIABLE');
// => "MyVariableValue"

setVariable(name,value)

Sets the value of a Property Manager user-defined variable. Only variables that start with a PMUSER prefix are available. For more information see User-defined variables.

Review the table for information about the possible arguments.

Argument Description Object Type
name Names of the user-defined variable to set String
value Value to assign to the variable String
Note: The total size limit when creating Property Manager user-defined variables is 1024 characters. This limit includes the name and value of the variables and only applies to EdgeWorkers added or modified using the JavaScript API.
// PMUSER_MyVariable = "MyValue"
request.setVariable('PMUSER_MyVariable',’MyNewValue’);
// => PMUSER_MyVariable = "MyNewValue"

route(destination)

Routes the outbound origin request to a predefined 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. Can only be modified during the onClientRequest event.

Review the table below for information about the possible arguments.
Argument Description Object Type
path New path for the outbound origin request. Include the complete URI path beginning with a forward slash (/). If the URI includes a filename, the file extension is also required.
Note: This setting overrides any previously defined Property Manager behaviors that attempt to set the forward path.
Note: A Property Manager behavior that attempts to set the forward path after the EdgeWorker runs, will override this EdgeWorkers setting.
String
query New query string for the outbound origin request. String
origin New outbound origin request to a preconfigured origin identifier in the delivery property.
Note: See Set up a Conditional Origin Group rule in the Cloudlets documentation for instructions on how to set up the origins to use with your EdgeWorkers.
String
Note: You may have different 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.
// GET /search?q=something
request.route({origin: "origin-1", path: "/example.html", query: "a=1&b=2"}); 
// GET /example.html?a=1&b=2