HttpResponse PRO

Requires Scripting PRO

The HttpResponse class represents an HTTP response object returned by the server to the client. It defines the response’s status code, headers, and body, and provides convenient factory methods for creating common HTTP responses (e.g., ok, notFound, internalServerError).

HttpResponse is typically used together with HttpResponseBody to send text, HTML, binary data, or files back to the client.


Overview

HttpResponse provides:

  • Easy creation of standard HTTP responses (200, 404, 500, etc.)
  • Support for custom status codes and reason phrases
  • Ability to return text, binary, HTML, or file data
  • Custom response headers
  • Integration with FileEntity and Data for flexible body content

Properties

statusCode: number

The numeric HTTP status code.

Example:

1const res = HttpResponse.ok(HttpResponseBody.text("OK"))
2console.log(res.statusCode)
3// Output: 200

reasonPhrase: string

The reason phrase associated with the status code (e.g., "OK", "Not Found", "Internal Server Error").

Example:

1console.log(res.reasonPhrase)
2// Output: "OK"

Methods

headers(): Record<string, string>

Returns the headers of the response as a key–value object.

Example:

1const res = HttpResponse.ok(HttpResponseBody.text("Hello"))
2console.log(res.headers())

static ok(body: HttpResponseBody): HttpResponse

Creates a 200 OK response.

Parameters:

Name Type Description
body HttpResponseBody The response body (use HttpResponseBody.text(), data(), or html()).

Example:

1return HttpResponse.ok(HttpResponseBody.text("Hello, world!"))

static created(): HttpResponse

Returns a 201 Created response, indicating that a new resource was successfully created.

Example:

1return HttpResponse.created()

static accepted(): HttpResponse

Returns a 202 Accepted response, indicating the request was accepted but not yet processed.

Example:

1return HttpResponse.accepted()

static movedPermanently(url: string): HttpResponse

Returns a 301 Moved Permanently redirect response.

Parameters:

Name Type Description
url string The target URL to redirect to.

Example:

1return HttpResponse.movedPermanently("https://example.com/new-page")

static movedTemporarily(url: string): HttpResponse

Returns a 302 Moved Temporarily redirect response.

Parameters:

Name Type Description
url string The temporary redirect target URL.

Example:

1return HttpResponse.movedTemporarily("https://example.com/login")

static badRequest(body?: HttpResponseBody | null): HttpResponse

Returns a 400 Bad Request response, indicating invalid parameters or malformed input.

Parameters:

Name Type Description
body HttpResponseBody? Optional error body describing the issue.

Example:

1return HttpResponse.badRequest(HttpResponseBody.text("Invalid parameters"))

static unauthorized(): HttpResponse

Returns a 401 Unauthorized response, indicating authentication is required.

Example:

1return HttpResponse.unauthorized()

static forbidden(): HttpResponse

Returns a 403 Forbidden response, indicating the request is understood but not allowed.

Example:

1return HttpResponse.forbidden()

static notFound(): HttpResponse

Returns a 404 Not Found response when the requested resource does not exist.

Example:

1return HttpResponse.notFound()

static notAcceptable(): HttpResponse

Returns a 406 Not Acceptable response, indicating the request’s content type is unsupported.

Example:

1return HttpResponse.notAcceptable()

static tooManyRequests(): HttpResponse

Returns a 429 Too Many Requests response, indicating the client is sending requests too quickly.

Example:

1return HttpResponse.tooManyRequests()

static internalServerError(): HttpResponse

Returns a 500 Internal Server Error response, indicating an unexpected server error occurred.

Example:

1return HttpResponse.internalServerError()

static raw(statusCode: number, phrase: string, options?: { headers?: Record<string, string>; body?: Data | FileEntity } | null): HttpResponse

Creates a fully custom response with a specific status code, reason phrase, headers, and body.

Parameters:

Name Type Description
statusCode number The HTTP status code.
phrase string The reason phrase.
options.headers Record<string, string> Optional custom headers.
options.body Data | FileEntity Optional response body (binary data or file).

Example:

1const file = FileEntity.openForReading(Path.join(Script.directory, "image.png"))
2return HttpResponse.raw(200, "OK", {
3  headers: { "Content-Type": "image/png" },
4  body: file
5})

Usage with HttpResponseBody

HttpResponseBody.text(text: string)

Returns a plain-text response body.

1HttpResponse.ok(HttpResponseBody.text("Hello, world"))

HttpResponseBody.html(html: string)

Returns an HTML response body.

1HttpResponse.ok(HttpResponseBody.html("<h1>Welcome</h1>"))

HttpResponseBody.data(data: Data)

Returns a binary data response body.

1const data = Data.fromRawString("Binary content", "utf-8")
2HttpResponse.ok(HttpResponseBody.data(data))

Full Examples

1. Returning a JSON response

1server.registerHandler("/user", (req) => {
2  const json = JSON.stringify({ name: "Alice", age: 25 })
3  const data = Data.fromRawString(json, "utf-8")
4  return HttpResponse.ok(HttpResponseBody.data(data))
5})

2. File download response

1server.registerHandler("/download", (req) => {
2  const file = FileEntity.openForReading(Path.join(Script.directory, "example.zip"))
3  return HttpResponse.raw(200, "OK", {
4    headers: { "Content-Type": "application/zip" },
5    body: file
6  })
7})

3. Error handling response

1server.registerHandler("/api", (req) => {
2  if (req.method !== "POST") {
3    return HttpResponse.badRequest(HttpResponseBody.text("POST method required"))
4  }
5  return HttpResponse.ok(HttpResponseBody.text("Success"))
6})