HttpResponseBody PRO

Requires Scripting PRO

The HttpResponseBody class represents the body content of an HTTP response. It can contain text, HTML, binary data, or other forms of content. HttpResponseBody is typically used with the HttpResponse class to return formatted data to the client.


Overview

When building custom HTTP endpoints with HttpServer, the response body defines what content the client actually receives. HttpResponseBody provides convenient static factory methods to construct various types of response content:

  • Plain text (text)
  • HTML content (html, htmlBody)
  • Binary data (data)

Common Use Cases

  • Returning plain text (e.g., simple API messages)
  • Returning HTML pages for browser display
  • Returning binary data such as images, JSON files, or downloadable archives

Static Methods

static text(text: string): HttpResponseBody

Creates a plain-text response body.

Parameters:

Name Type Description
text string The text content to include in the response body.

Example:

1const body = HttpResponseBody.text("Hello, world")
2return HttpResponse.ok(body)

Response example:

HTTP/1.1 200 OK Content-Type: text/plain Hello, world

static data(data: Data): HttpResponseBody

Creates a response body containing binary data.

Parameters:

Name Type Description
data Data The binary data object to send in the response body.

Example:

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

This is useful for sending files, images, or JSON payloads as binary data.


static html(html: string): HttpResponseBody

Creates an HTML response body (standard HTML document).

Parameters:

Name Type Description
html string The HTML markup to include in the response body.

Example:

1const html = `
2<html>
3  <head><title>Hello</title></head>
4  <body><h1>Welcome to Scripting Server</h1></body>
5</html>
6`
7return HttpResponse.ok(HttpResponseBody.html(html))

When accessed in a browser, the response is rendered as a web page.


static htmlBody(html: string): HttpResponseBody

Creates an HTML body-only response. Similar to html(), but may exclude full document structure (<html>, <body>, etc.). This method is often used for partial HTML rendering or embedded HTML fragments.

Parameters:

Name Type Description
html string The HTML snippet or body content.

Example:

1return HttpResponse.ok(HttpResponseBody.htmlBody("<h1>Inline HTML Body</h1>"))

Example Use Cases

1. Return a plain-text response

1server.registerHandler("/hello", (req) => {
2  return HttpResponse.ok(HttpResponseBody.text("Hello from server"))
3})

2. Return an HTML page

1server.registerHandler("/", (req) => {
2  const html = `
3  <html>
4    <head><title>Home</title></head>
5    <body>
6      <h1>Welcome</h1>
7      <p>This is a simple Scripting HTTP server.</p>
8    </body>
9  </html>`
10  return HttpResponse.ok(HttpResponseBody.html(html))
11})

3. Return a binary file (e.g., an image)

1server.registerHandler("/image", (req) => {
2  const fileData = FileManager.readAsData(Path.join(Script.directory, "logo.png"))
3  return HttpResponse.ok(HttpResponseBody.data(fileData))
4})

4. Return a partial HTML fragment

1server.registerHandler("/partial", (req) => {
2  return HttpResponse.ok(HttpResponseBody.htmlBody("<div>Partial Content</div>"))
3})

Summary

Method Description Typical Use Case
text() Returns plain text content API responses, logs
data() Returns binary data Files, JSON, images
html() Returns a full HTML document Web pages
htmlBody() Returns an HTML fragment Template rendering or partial updates