createResponse()

Generates the return value for responseProvider().

Use this function to validate the passed values and return an opaque object. The opaque object can be used to resolve the Promise returned from responseProvider(). The function accepts either a list of arguments or an options object.
Tip: You need to enable the multi-part response header for responseProvider when adding the built-in log module to your EdgeWorker functions. For more information see Enable and view JavaScript Logging for responseProvider.
Note: An exception is thrown if callers specify invalid arguments.
Review the table for information about the possible arguments.
Argument Description Object Type
status HTTP status code of the outgoing response Integer
headers Properties used as key:value pairs for the response headers

Keys are strings that contain header names, values are either strings or arrays of strings.

Note: The status supports 2xx Success, 3xx Redirection, 4xx Client Error, and 5xx Server Error status codes.
Object
Note: A string cannot contain illegal charcters. Refer to section three of the RFC 7230 document for information about the allowed characters.
body Content of the response body String or ReadableStream

When specified as a string, the body is limited to 100,000 bytes.

When specified as a ReadableStream, there is no size limit. The ReadableStream should consist of bytes of data. You can use the TextEncoder class to transform a text stream into a byte stream.

deny_reason [optional] Deny reason when the status code is a 403 String
import { createResponse } from 'create-response';
export function responseProvider (request) {
  return Promise.resolve(createResponse(200, { 'Powered-By': ['Akamai EdgeWorkers'] } 
    , '<html><body><p>Hello World</p></body></html>'));
}
When specifying an options object, the arguments are:
Argument Description Object Type
body Contents of the response body String or ReadableStream

When specified as a string, the body is limited to 100,000 bytes.

When specified as a ReadableStream, there is no size limit. The ReadableStream should consist of bytes of data. You can use the TextEncoder class to transform a text stream into a byte stream.

options (optional, Object) Optional values that the user can choose to specify. The definition of each property is the same as above.
options.status Defaults to 200 (optional) Number
options.headers Defaults to {} (optional) Object
options.denyReason Defaults to "" (optional) String
import { createResponse } from 'create-response';
export function responseProvider (request) {
  const options = {};
  options.status = 200;
  options.headers = { header1: 'value1', header2: 'value2' };
  return Promise.resolve(createResponse('<html><body><p>Hello World</p></body></html>', options));
}