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.

Properties for the Request Object

method

The HTTP method of the incoming request. This is a read-only string value.

// GET /search?q=something 
request.method 
// => "GET"

scheme

The scheme of the incoming request ("http" or "https"). This is a read-only string value.

// https://www.example.com/search?q=something
request.scheme
// => "https"

host

The host header value of the incoming request. This is a read-only string value.

// Host: www.example.com 
request.host   
// => "www.example.com"

path

The URL path of the incoming request, including the filename and extension, but without any query string. This is a read-only string value.

// https://www.example.com/search?q=something
request.path 
// => "/search"

query

The query string of the incoming request. This is a read-only string value.

// GET /search?q=something
request.query
// => "q=something"

url

The relative path and query string of the incoming request. This is a read-only string value.

// https://www.example.com/search?q=something
request.url  
// => "/search?q=something"

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.

// Request cpCode 12345
 request.cpCode 
// => 12345

userlocation

Returns the object that contains properties specifying the geographic location.

For more information see the User Location Object .

device

Returns information that specifies client device characteristics.

For more information see the Device Object.

cacheKey

Returns the methods that specify the HTTP response in cache for an HTTP request.

For more information see the cacheKey Object.

Methods for the Request Object

The following methods are available for the EdgeWorkers Request objects.

Review this table to view the supported event handlers for each EdgeWorkers request object method.

Method supported event handlers
respondWith(status, headers, body,[deny_reason])

onClientRequest

onOriginResponse

addHeader(name, value)

onClientRequest

onOriginResponse

getHeader(name)

onClientRequest

onOriginRequest

onOriginResponse

onClientResponse

responseProvider

getHeaders() responseProvider
Note: This method supports request headers in the responseProvider event handler. It can only be used to copy the request headers into your sub-request.
setHeader(name, value)

onClientRequest

onOriginRequest

removeHeader(name)

onClientRequest

onOriginRequest

getVariable(name)

onClientRequest

onOriginRequest

onOriginResponse

onClientResponse

responseProvider

setVariable(name,value)

onClientRequest

onOriginRequest

onOriginResponse

onClientResponse

route(destination) onOriginRequest

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');
// => {} 

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

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"

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.

This request header can only be modified during the responseProvider event.

import { httpRequest } from 'http-request';
import { createResponse } from 'create-response';
 
export function responseProvider(request) {
    let hdrs = request.getHeaders();
    httpRequest('/header-collector', {headers: hdrs});
 
    //...
}

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

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
Note: PMUSER_ variables with a security setting of sensitive are not available within an EdgeWorker using getVariable.
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.
Note: PMUSER_ variables with a security setting of sensitive are not available within an EdgeWorker using setVariable.
// 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