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.
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 an denied API response
request.respondWith(403, {'Content-Type': ['application/json;charset=utf-8']  }, '{}', 'Denied Response');
// => {} 
Review the table below 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
getHeader(name) Returns an array of header values by header name. The header names are case insensitive. If the header doesn’t exist, an empty string is returned. Request headers can only be modified during onClientRequest and onOriginRequest events.
// 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.

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.foo.com')
// Referer: http://www.foo.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.
//
request.addHeader('Foo', 'bar')
// Foo: bar
removeHeader(name) Remove the named header. Request headers can only be modified during onClientRequest and onOriginRequest events. The header name is case insensitive.
// Pragma: foo
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.
request.getVariable('PMUSER_FOO')
// => "bar"

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.

// GET /search?q=something
 
request.route({origin: "origin-1", path: "/foo.html", query: "a=1&b=2"})
 
// GET /foo.html?a=1&b=2
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. 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