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.

To view debugging information for HTTP sub-requests you need to enable enhanced debug headers. For more information review the Enhanced debug header details for HTTP sub-requests section.

These restrictions apply when using the httpRequest function:
  • Requests made through the httpRequest() function will not trigger EdgeWorkers attached to the requested resource.
  • EdgeWorkers sub-requests only support HTTPS. If you specify another protocol in the sub-request the EdgeWorkers function will automatically convert it to HTTPS.
  • You cannot specify a port number.
  • You cannot currently use Sandbox to test EdgeWorkers that contain the httpRequest() function.
  • HTTP requests made during the onClientRequest event only support the GET and HEAD methods.
Note: For more information you can also review the limitations that apply to sub-requests.

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);
  }
}