Skip to content

$.response

$.response represents the HTTP response currently being populated. This API is used for returning a result response to the xsk HTTP service caller.

Overview

Sample Usage

// array of cars and their colors
let cars = [
  { make: "mercedes", color: "red" },
  { make: "audi", color: "blue" },
  { make: "toyota", color: "red" },
  { make: "vw", color: "blue" }
]

// filter by color function
function filterCarsByColor(color) {
  return cars.filter(function(car) { 
    return car.color === color;
  })
}

if ($.request.method === $.net.http.GET) {
  // get query parameter color
  let color = $.request.parameters.get("color");

  // handle some request operation 
  if (color) {
    // filter by color if passed
    let filteredCars = filterCarsByColor(color);

    // send response with filtered cars by color
    $.response.contentType = "application/json";
    $.response.status = $.net.http.OK;
    $.response.setBody(JSON.stringify({
      "cars": filteredCars
    }));
  } else {
    // send response with all cars as color param is missing
    $.response.status = $.net.http.BAD_REQUEST;
    $.response.setBody(JSON.stringify({
      "cars": cars
    }));
  }
} else {
  // unsupported method
  $.response.status = $.net.http.NOT_FOUND;
  $.response.setBody(JSON.stringify({
    "error": "not found"
  }));
}

Properties

Name Description Type
body The body of the response. $.web.Body
cacheControl Easy access to the cache control header of the entity. string
contentType The content type of the entity. string
cookies The cookies associated with the entity. $.web.TupelList
entities The sub-entities of the entity. $.web.EntityList
headers The headers of the entity. $.web.TupelList
status The HTTP status code of the outgoing HTTP response. $.net.http

Functions

Function Description Returns
followUp(object) Enable running a follow up job that executes in the background. -
setBody(body) Sets the body of the entity. -