Request

The `Request` class, parameter packaged and passed to the script when executing onRequest

Constructor

new Request(init)

Creates a new instance of the Request class.
Parameters:
NameTypeDescription
initobjectOptional. An initial object to populate the request.
Properties
NameTypeAttributesDefaultDescription
init.schemestring<optional>
""The scheme of the request.
init.hoststring<optional>
""The host of the request.
init.portnumber<optional>
0The port of the request.
init.pathstring<optional>
""The path of the request.
init.methodstring<optional>
""The method of the request.
init.queriesstring<optional>
""The queries of the request.
init.headersArray<optional>
[]The headers of the request.
init.textBodystring<optional>
nullThe text body of the request.
Example
// Creating a new instance of the Request class with custom values
const request = new Request({
  scheme: 'https',
  host: 'example.com',
  port: 8080,
  path: '/api',
  method: 'GET',
  queries: 'param1=value1&param2=value2',
  headers: [
    ['Content-Type', 'application/json'],
    ['Authorization', 'Bearer token']
  ],
  textBody: '{"key": "value"}'
});
console.log(request.encode());

Members

body

Gets or sets request body.
Properties
NameTypeDescription
setterstring | Map | Array | URLSearchParamsSets request body with typeof {string | Map | Array | URLSearchParams}.
getterBodyGets request body as type {Body}.
Examples
// Getting the body of the request
console.log(request.body); // Output: Body { ... }
// Setting the body of the request using a string
request.body = 'This is the request body as a string';
console.log(request.body.parseText());
// Setting the body of the request using JSON data
const jsonData = { key: 'value', nested: { foo: 'bar' } };
request.body = JSON.stringify(jsonData);
console.log(request.body.parseText());
// Setting the body of the request using URLSearchParams
const urlSearchParams = new URLSearchParams();
urlSearchParams.append('param1', 'value1');
urlSearchParams.append('param2', 'value2');
request.body = urlSearchParams;
console.log(request.body.parseText());

headers

Gets the headers of the request.
Properties
NameTypeDescription
getterHeadersGets the headers of the request.
Example
// Getting the headers of the request
console.log(request.headers); // Output: Headers { ... }

host

Sets or gets the host of the request.
Properties
NameTypeDescription
getterstringGets the host of the request.
setterstringSets the host of the request.
Examples
// Getting and setting properties of the request
console.log(request.host); // Output: example.com
// Setting the host of the request
request.host = 'example.org';
console.log(request.host); // Output: example.org

method

Gets or sets the method of the request.
Properties
NameTypeDescription
getterstringGets the method of the request.
setterstringSets the method of the request.
Examples
// Getting the method of the request
console.log(request.method); // Output: GET
// Setting the method of the request
request.method = 'POST';
console.log(request.method); // Output: POST

path

Sets or sets the path of the request.
Properties
NameTypeDescription
getterstringGets the path of the request.
setterstringSets the path of the request.
Examples
// Getting and setting properties of the request
console.log(request.path); // Output: /api
// Setting the path of the request
request.path = '/new';
console.log(request.path); // Output: /new

port

Sets or gets the port of the request.
Properties
NameTypeDescription
getternumberGets the port of the request.
setternumberSets the port of the request.
Examples
// Getting and setting properties of the request
console.log(request.port); // Output: 8080
// Setting the port of the request
request.port = 3000;
console.log(request.port); // Output: 3000

queries

#Gets the queries of the request.
Properties
NameTypeDescription
getterURLSearchParamsGets the queries of the request.
Example
// Getting the queries of the request
console.log(request.queries); // Output: URLSearchParams { ... }

scheme

Sets the scheme of the request.
Properties
NameTypeDescription
getterstringGets the scheme of the request.
setterstringSets the scheme of the request.
Examples
// Getting and setting properties of the request
console.log(request.scheme); // Output: https
// Setting the scheme of the request
request.scheme = 'http';
console.log(request.scheme); // Output: http