Back to Kodaris

Request

Overview

The Request module provides access to information about the current HTTP request being handled by your server-side code. It includes details such as the request path, method, path parameters, query parameters, headers, body content, and client metadata.

This module is essential for building dynamic behavior based on the request context—such as routing logic, input validation, filtering, and access control.

The Request module is globally available in the platform environment via kd.request. There is no need to import or initialize it manually.

Key Features

  • Path & Method Access - Retrieve the full request path and HTTP method (GET, POST, etc.).
  • Path Parameters - Access dynamic segments of the URL path (e.g., /users/:id → id).
  • Query Parameters - Get URL query parameters for filtering, pagination, or other inputs.
  • Headers - Inspect incoming HTTP headers for custom values or content negotiation.
  • Body - Parse JSON or form data submitted in the request payload.
  • IP & Origin Info - Obtain client IP addresses or origin metadata for logging or access control.

Example Use Cases

  • Routing logic based on request method or path
  • Extracting identifiers from URL path parameters
  • Handling filters, pagination, or search terms from query strings
  • Validating and processing request payloads

Methods

getPathParam(index)

Retrieves a specific path parameter by its index position.

  1. Parameters
    1. index (Number): The index of the path parameter to retrieve
  2. Returns
    1. String | null: The path parameter at the specified index, or null if not found

Example

// For URL /users/123/profile
var users = kd.request.getPathParam(0);
// users = "users"

var userId = kd.request.getPathParam(1);
// userId = "123"

var section = kd.request.getPathParam(2);
// section = "profile"

var nonExistent = kd.request.getPathParam(5);
// nonExistent = null

getPathParams()

Returns all path parameters as an array.

  1. Parameters: None
  2. Returns: Array: An array of all path parameters

Example:

// For URL /users/123/profile
const params = kd.request.getPathParams();
// params = ["users", "123", "profile"]

getProperty(key)

The Kodaris platform will inject data into the current request as needed based on the request type. Use this method to get the value of a data property that was injected.

  1. Parameters
    1. key (String): The key of the property to return
  2. Returns: Any: The value of the request property
  3. Example:
// For URL /content/about
var isContentPage = kd.request.getProperty("isContentPage");
// isContentPage = true

getBody()

Retrieves the body/payload of the current HTTP request. For JSON content types, the body is automatically parsed into an object. Returns null if the request has no body or if the body cannot be parsed.

  1. Parameters: None
  2. Returns: Object | String | null: The request payload or null if none exists
  3. Example:
const payload = kd.request.getBody();
// payload = { "email": "john@example.com" }

getParameter(key)

Retrieves a specific request query parameter.

  1. Parameters: None
  2. Returns: String | null: The value of the parameter
  3. Example:
// For URL ?q=searchterm&page=2
const searchQuery = kd.request.getParameter('q');
// searchQuery = "searchterm"

const page = parseInt(kd.request.getParameter('page')) || 1;
// page = 2

const missing = kd.request.getParameter('nonexistent');
// missing = null

hasParameter(key, [value])

Checks if a parameter exists and optionally if it has a specific value.

  1. Parameters:
    1. key (String): The parameter name to check
    2. value (String, optional): The specific value to check for
  2. Returns: Boolean: true if the parameter exists with the specified value (if provided), otherwise false
  3. Example:
// For URL ?filter=active&status=published&view=detailed
// Check if filter parameter exists
const hasFilter = kd.request.hasParameter('filter');
// hasFilter = true

// Check if status parameter equals "published"
const isPublished = kd.request.hasParameter('status', 'published');
// isPublished = true

// Check if status parameter equals "draft"
const isDraft = kd.request.hasParameter('status', 'draft');
// isDraft = false

// Check if a non-existent parameter exists
const hasSort = kd.request.hasParameter('sort');
// hasSort = false

getParameters()

Retrieves all request parameters as an object.

  1. Parameters: None
  2. Returns: Boolean: true if the parameter exists with the specified value (if provided), otherwise false
  3. Example:
// For URL ?limit=20&offset=40&sort=name
const allParams = kd.request.getParameters();
// allParams = { "limit": ["20"], "offset": ["40"], "sort": ["name"] }

getTemplate()

Many request urls are natively handled by the Kodaris platform. For example, when a user visits the category page on your website, the platform fetches the requested product category from the database along with its products. Once the data is loaded, the platform will turn the rendering over to the website so you can control how the category and its products are displayed to the user. The platform will provide the EJS template file you should use to render the category page. You can get the EJS template file path the platform wants you to render by calling the getTemplate() method.

  1. Parameters: None
  2. Returns: String: The file path of the EJS template that should be used to render the current request
  3. Example:
// For URL /category/drywall
var template = kd.request.getTemplate();
// template = /src/product/category.ejs

set(key, value)

Sets a custom value in the request context for later retrieval.

  1. Parameters:
    1. key (String): The name for storing the value
    2. value (Any): The value to store
  1. Returns: None
  2. Example:
// Store the start time of processing
kd.request.set('processingStart', Date.now());
// No return value

get(key)

Sets a custom value in the request context for later retrieval.

  1. Parameters:
    1. key (String): The name of the stored value to retrieve
  2. Returns: Any | null: The stored value, or null if not found
  1. Example:
// Calculate processing time
var startTime = kd.request.get('processingStart');
// startTime = 1618324567890

// Getting a non-existent value
var noValue = kd.request.get('nonexistent');
// noValue = null

exists()

Checks if the there is a current request. For some server-side javascript contexts there is no current request. For example, an automated scheduled task that runs every 5 minutes to send order delivered notifications for any orders that have been recently delivered. Such a scheduled task is initiated by the Kodaris platform and is not initiated by an HTTP request. Thus, there will be now request in the context. You can use the exists() method to see if there is an active request or not before executing request specific logic.

  1. Parameters: None
  2. Returns: Boolean: Whether the request object exists
  3. Example:
const requestExists = kd.request.exists();
// requestExists = true (in a normal request context)

if (kd.request.exists()) {
  // Process the request
} else {
  console.log('No active request to process');
}

getPath()

Checks if the there is a current request. For some server-side javascript contexts there is no current request. For example, an automated scheduled task that runs every 5 minutes to send order delivered notifications for any orders that have been recently delivered. Such a scheduled task is initiated by the Kodaris platform and is not initiated by an HTTP request. Thus, there will be now request in the context. You can use the exists() method to see if there is an active request or not before executing request specific logic.

  1. Parameters: None
  2. Returns: String: The path part of the request URL
  3. Example:
var path = kd.request.getPath();
// path = "/products/electronics/phones"

// Basic routing
if (path.startsWith('/myapp/dashboard')) {
  // Handle myapp requests
} else if (path.startsWith('/promotions/')) {
  // Handle promotion requests
}

getUrl()

Checks if the there is a current request. For some server-side javascript contexts there is no current request. For example, an automated scheduled task that runs every 5 minutes to send order delivered notifications for any orders that have been recently delivered. Such a scheduled task is initiated by the Kodaris platform and is not initiated by an HTTP request. Thus, there will be now request in the context. You can use the exists() method to see if there is an active request or not before executing request specific logic.

  1. Parameters: None
  2. Returns: String: The full URL of the request
  3. Example:
var url = kd.request.getUrl();
// url = "https://commerce.kodaris.com/content/about
In this article