> For clean Markdown of any page, append .md to the page URL. > For a complete documentation index, see https://akamai.ferndocs.com/edge-workers/request-object/llms.txt. > For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://akamai.ferndocs.com/_mcp/server. # 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. ```javascript const opt = { "method": "POST", "body": request.body.pipeThrough(new TextEncoderStream()) } let response = await httpRequest(url, opt); ``` ## cacheKey Returns the methods that specify the HTTP response in cache for an HTTP request. For more information see the [CacheKey Object](/edge-workers/cachekey-object). ## clientIp The original client IP address that can either be IPv4 or IPv6. This is a read-only string value. ```javascript //Request clientIp request.clientIp // => IPv4 or IPv6 ``` ## 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. ```javascript // Request cpCode 12345 request.cpCode // => 12345 ``` ## device Returns an object that contains properties specifying the client device characteristics. For more information see the [Device Object](/edge-workers/device-object). ## host The `host` header value of the incoming request from the client. This is a read-only string value. ```javascript // Host: www.example.com request.host // => "www.example.com" ``` ## method The HTTP `method` of the incoming request. This is a read-only string value. ```javascript // GET /search?q=something request.method // => "GET" ``` ## 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 `/`. ```javascript // 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. If the incoming request does not specify a query, the value is an empty string. ```javascript // GET /search?q=something request.query // => "q=something" ``` ## scheme The `scheme` of the incoming request ("http" or "https"). This is a read-only string value. ```javascript // https://www.example.com/search?q=something request.scheme // => "https" ``` ## url The relative path and query string of the incoming request. This is a read-only string value. ```javascript // https://www.example.com/search?q=something request.url // => "/search?q=something" ``` ## userLocation Returns an object that contains properties specifying the geographic location. For more information see the [User Location Object](/edge-workers/user-location-object). # Methods The following methods are available for the EdgeWorkers Request Object. | Methods | onClient Request | onOrigin Request | onOrigin Response | onClient Response | response Provider | | :-------------------------------------------------- | :--------------- | :--------------- | :---------------- | :---------------- | :---------------- | | [respondWith()](/edge-workers/request-object#respondwith) | ✓ | ✓ | ✓ | ✓ | | | [addHeader()](/edge-workers/request-object#addheader) | ✓ | ✓ | | | | | [getHeader()](/edge-workers/request-object#getheader) | ✓ | ✓ | ✓ | ✓ | ✓ | | [getHeaders()](/edge-workers/request-object#getheaders) | ✓ | ✓ | ✓ | ✓ | ✓ | | [setHeader()](/edge-workers/request-object#setheader) | ✓ | ✓ | | | | | [removeHeader()](/edge-workers/request-object#removeheader) | ✓ | ✓ | | | | | [getVariable()](/edge-workers/request-object#getvariable) | ✓ | ✓ | ✓ | ✓ | ✓ | | [setVariable()](/edge-workers/request-object#setvariable) | ✓ | ✓ | ✓ | ✓ | | | [route()](/edge-workers/request-object#route) | ✓ | | | | | | [text()](/edge-workers/request-object#text) | | | | | ✓ | | [json()](/edge-workers/request-object#json) | | | | | ✓ | | [arrayBuffer()](/edge-workers/request-object#arraybuffer) | | | | | ✓ | | [wasTerminated()](/edge-workers/request-object#wasterminated) | ✓ | ✓ | | | | ## 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`, and `Transfer-Encoding` hop-by-hop headers should not be set when creating a request. > > The `Host`, `Content-Length`, and `Vary` headers 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 `Host` header can break your request. > - An incorrect value in the `Content-Length` header will break the response. Make sure that the value reflects the actual length of the payload you're passing. > - An incorrect value in the `Vary` header can break cacheability. > 📘 `respondWith()` supports the GET, POST, DELETE, PUT, PATCH, and HEAD request methods. #### Parameters ```javascript respondWith(status, headers, body, [deny_reason]) ``` Review the table for information about the available parameters. This method can be modified during the `onClientRequest`, `onClientResponse`, `onOrigin Request`, and `onOrigin Response` events. | Parameter | Type | Description | | --- | --- | --- | | status | Integer | HTTP status code
Note: The status supports 2xx Success , 3xx Redirection , 4xx Client Error , and 5xx Server Error status codes. An exception is thrown if the status code is outside the 2xx to 5xx range.)
| | headers | Object | Properties used as key:value pairs for the response header | | body | String | Content of the response body | | deny_reason | String | (optional) Deny reason when the status code is a 403 | > 📘 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](cookies.md) 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. ```javascript // generate an empty API response request.respondWith(200, {'Content-Type': ['application/json;charset=utf-8'] }, '{}'); // => {} ``` ```javascript // generate a denied API response request.respondWith(403, {'Content-Type': ['application/json;charset=utf-8'] }, '{}', 'Denied Response'); // => {} ``` ## 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 `onClientRequest`and `onOriginRequest`events. #### Parameters ```javascript addHeader(name, value) ``` Review the table for information about the available parameters. | Parameter | Type | Description | | --- | --- | --- | | name | String | New header nameInclude the complete URI path beginning with a forward slash (/). If the URI includes a filename, the file extension is also required.
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.
| | query | String | New query string for the outbound origin request. | | origin | String | New outbound origin request to a preconfigured origin identifier in the delivery property.Note: See [Set up a Conditional Origin Group rule](https://techdocs.akamai.com/cloudlets/docs/about-conditional-origins#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.
| > 📘 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](https://techdocs.akamai.com/property-mgr/docs/configure-unique-origin#cache-key-hostname) documentation. ```javascript // GET /search?q=something request.route({origin: "origin-1", path: "/example.html", query: "a=1&b=2"}); // GET /example.html?a=1&b=2 ``` ## 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](best-practices-for-asynchronous-processing.md) is rejected. This method can only be called during the `responseProvider` event. ```javascript import { createResponse } from 'create-response'; export async function responseProvider (request) { let mybody = await request.text(); return createResponse(200, { 'request-body': [mybody] }, 'Hello World
' ); } ``` > 📘 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](best-practices-for-asynchronous-processing.md) is rejected. This method can only be called during the `responseProvider` event. ```javascript import {createResponse} from 'create-response'; export async function responseProvider (request) { let mybody = await request.json(); return createResponse(200, {}, JSON.stringify(mybody)); } ``` > 📘 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](best-practices-for-asynchronous-processing.md) is rejected. This method can only be called during the `responseProvider` event. ```javascript import { createResponse } from 'create-response'; export function responseProvider(request) { return request.arrayBuffer().then(function(ab) { // Sum the bytes in the array let buf = new Uint8Array(ab); let total = buf.reduce((total, cur) => total + cur); return createResponse(200, {}, `Bytes add up to ${total}`); }); } ``` 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. ```javascript function somethingComplicated(request) { if (0 <= request.query.search(/stop/)) { request.respondWith(242, {}, "somethingComplicated found stop"); } } export function onClientRequest(request) { somethingComplicated(request); // The caller uses wasTerminated() to see if a response to the request // has been generated earlier in execution handler. if (!request.wasTerminated()) { request.respondWith(200, {}, "Nothing complicated happened"); } } ```