httpRequest()

Exports a function from a built-in module called http-request.

httpRequest() returns a Promise that resolves to an HttpResponse Object. This function is similar to fetch from the Fetch API.

See the HTTP sub-request limitations section for more information about limitations that apply to HTTP sub-requests.

To view debugging information for HTTP sub-requests you need to enable enhanced debug headers. For more information review the Response Headers for HTTP sub-requests section.
Note: Requests through the httpRequest() function will not trigger EdgeWorkers attached to the requested resource.

Only HTTPS is supported for the scheme. Specifying port numbers is not supported.
Note: You cannot currently use Sandbox to test EdgeWorkers that contain the httpRequest() function.
Note: HTTP requests made during the onClientRequest event only support the GET and HEAD methods.

httpRequest(url [,options])

The url is the absolute or relative URL to fetch. Relative URLs use the parent request as the base URL. This is a string value.

The domain of a requested URL must be served by the Akamai platform.

httpRequest('https://www.example.com')
The options must be a JavaScript object containing at least one of the following optional properties, method, headers, body, or timeout.
Property Description Property type
method The HTTP request method to use.

Defaults to GET, if not specified.

String
headers The HTTP request headers to include. The property names of this object are the header names. The property values are arrays containing the header values. Object
Note: EdgeWorkers should not manipulate Akamai headers added for internal use. These headers typically start with ‘X-Ak’ and ‘Akamai-’.
body Content of the request body. String
timeout Timeout value, in milliseconds, for the response to complete. Integer
const options = {}
 
options.method = "POST"
options.headers = { "Content-Type": "application/x-www-form-urlencoded" }
options.body = "field1=value1&field2=value2"
 
const response = await httpRequest(url, options)
const response = await httpRequest(url, {
   method: "POST",
   headers: { "Content-Type": "application/x-www-form-urlencoded" },
   body: "field1=value1&field2=value2"
})
const options = {}
options.method = "GET"
options.timeout = 100

const response = await httpRequest(url, options)
const response = await httpRequest(url, {
   method: "GET",
   timeout: 100
})
This example shows how to use an asynchronous encapsulating function to call an httpRequest() function.
async function getJSON (url) {
  const response = await httpRequest(`${url}`);
  if (response.ok) {
	return await response.json();
  } else {
	return { language: 'en', greeting: 'Hello World' };
  }
}
This example shows how to use the onClientRequest event handler to call an httpRequest() function. It is also async.
import { httpRequest } from 'http-request';
import { logger } from 'log';
export async function onClientRequest(request) {
  try {
    const response = await httpRequest('/ab_test/ab.json');
    logger.log('OnClientRequest SubRequest Successful');
      if (response.ok) {
       // Add logic process response 
      }
  } catch (error) {
    logger.log('OnClientRequest SubRequest Failed: %s', error);
  }
}