Back to Kodaris

Http

Overview

The Http module provides a simple and consistent way to make outgoing HTTP requests from within your Kodaris platform. It is designed to work seamlessly in the server-side environment and supports a wide range of use cases including calling external APIs, interacting with internal services, or integrating with third-party systems.

Whether you need to fetch data, post JSON payloads, send headers, or handle authentication, the Http module makes it easy to perform network requests in a clean and reliable way.

Note: The Http module is globally available via kd.http - no import or setup required.

Methods

fetch(options)

  1. Parameters
    1. options (Object): The configuration for the HTTP request with the following properties:
      1. version (Number): Always set this to 2
      2. url (String, required): The URL to send the request to.
      3. method (String, required): The HTTP method to use. Valid values are: 'get', 'post', 'put', 'delete', 'patch', 'options', 'head', 'connect', 'trace' (case-insensitive).
      4. headers (Object, optional): HTTP headers to include with the request. Defaults to {'Content-Type': 'application/json'} if not provided.
      5. body (Any, optional): The request body data. If Content-Type is 'application/json' and the body is an object or array, it will be automatically stringified.
      6. queryParams (Object, optional): Query parameters to append to the URL
  2. Returns: (Object): With the following properties:
    1. status (Number): The HTTP status code of the response (or -1 if the request failed to execute).
    2. headers (Object): The response headers.
    3. body (Any): The response body. If the response content type is 'application/json', the body will be automatically parsed into a JavaScript object.
    4. errors (Array): Any errors that occurred during the request setup or execution.
  3. Error Handling
    1. The HTTP client performs validation checks before sending the request. If validation fails (e.g., missing required fields), the response will have:
      1. status: -1
      2. errors: Array containing error messages
      3. No actual HTTP request will be made
    2. When a request is made but returns an error status code, the status code and any error response from the server will be included in the returned object.
  4. FormData Support
    1. The client supports FormData for multipart form submissions. When using FormData:
      1. Set the Content-Type to 'multipart/form-data'
      2. Pass the FormData instance as the body
  5. Example
var response = kd.http.fetch({
  version: 2,
  url: 'https://commerce.kodaris.com/api/user/search/product',
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: {
    page: 0,
    size: 10,
    filterFields: [],
    queryFields: [],
    facetFields: []
  } 
});

/* Sample Response:
{
  status: 200,
  headers: {
    'content-type': 'application/json; charset=utf-8',
    'cache-control': 'no-cache',
    'content-length': '217',
    'date': 'Wed, 15 Mar 2023 10:23:45 GMT'
  },
  body: {
    content: [
      {code: "12LW", name: "12 inch Drywall", ...other fields}
      ... other products
    ]
  },
  errors: []
}

// Error response example:
{
  status: 400,
  headers: {
    'content-type': 'application/json; charset=utf-8',
    'content-length': '76',
    'date': 'Wed, 15 Mar 2023 10:23:45 GMT'
  },
  body: null,
  errors: ["Bad request, size is required"]
}

// Validation error example (no request sent):
{
  status: -1,
  headers: {},
  body: null,
  errors: ["method is required"]
}
In this article