Methods for the Request Object

The following methods are available for the EdgeWorkers Request objects.

respondWith(response) Constructs a response for the given request, rather than fetching a response from cache or the origin.
// 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.
// 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) Removes 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"