Skip to content

HTTP Response ​

The response object controls how MARS Engine APIs send responses to clients. It includes functions to set status codes, headers, content types, and caching behavior.

response.status(code: number) ​

Sets the HTTP status code for the response.

Example:

js
const id = param("id");
if(id == null){
  response.status(422);
  write('message', 'Missing parameter id');
  exit();
}

response.setCache(time: string | number) ​

Sets cache headers for the response.

  • Removes the "pragma" header
  • Sets Last-Modified, Cache-Control, Expires

time argument:

  • By default, time is in seconds.
  • Supports units: D (days), M (months), Y (years), W (weeks), s (seconds), m (minutes), h (hours).

Example:

js
let now = new DateTime();

// Cache for 60 seconds
response.setCache(60);
write('response', now.toString());

// Cache for 2 days
response.setCache('2D');
write('response', now.toString());

response.setContentType(type: string) ​

response.contentType(type: string) ​

response.setContentType(type: string) ​

Sets the Content-Type of the response.

  • Pass a MIME type or file extension. MARS Engine will map extensions to the appropriate MIME type.

Examples:

js
let text = 'Hello';

// Using MIME type
response.setContentType('text/plain');
write(text);

// Using extension
response.setContentType('html');
write(text);