HttpRequest PRO

Requires Scripting PRO

The HttpRequest class represents an HTTP request received by the server. It encapsulates all request information, including path, method, headers, body, client address, and parsed parameters. An instance of this class is provided as an argument to handler functions registered through HttpServer.registerHandler().


Overview

HttpRequest is typically used inside HTTP route handlers to:

  • Access request path, method, headers, and body
  • Read URL parameters and query parameters
  • Parse form data (application/x-www-form-urlencoded or multipart/form-data)
  • Verify authentication tokens or custom headers

Properties

path: string

The request path (excluding query parameters).

Example:

1console.log(request.path)
2// Output: "/api/user"

method: string

The HTTP method of the request (e.g., "GET", "POST", "PUT", "DELETE").

Example:

1console.log(request.method)
2// Output: "POST"

headers: Record<string, string>

An object containing all HTTP request headers as key–value pairs.

Example:

1console.log(request.headers["content-type"])
2// Output: "application/json"

body: Data

The request body as a Data object. You can convert it to a string using methods such as toRawString("utf-8").

Example:

1const text = request.body.toRawString("utf-8")
2console.log("Body content:", text)

address: string | null

The IP address of the client that made the request. If the address cannot be determined, this value is null.

Example:

1console.log("Client address:", request.address)

params: Record<string, string>

An object containing route parameters defined in the registered path.

Example:

1// Route registration
2server.registerHandler("/user/:id", (req) => {
3  const userId = req.params["id"]
4  return HttpResponse.ok(HttpResponseBody.text(`User ID: ${userId}`))
5})

If the request path is /user/123, the response will be:

User ID: 123

queryParams: Array<{ key: string; value: string }>

An array of query parameter key–value pairs extracted from the request URL.

Example:

1// Request URL: /search?keyword=apple&page=2
2for (const param of request.queryParams) {
3  console.log(param.key, "=", param.value)
4}
5// Output:
6// keyword = apple
7// page = 2

Methods

hasTokenForHeader(headerName: string, token: string): boolean

Checks whether the specified header contains the given token value. Commonly used for authentication headers such as Authorization.

Parameters:

Name Type Description
headerName string The name of the header to check (case-insensitive).
token string The token string to match.

Returns:

  • true if the header contains the token
  • false otherwise

Example:

1if (!request.hasTokenForHeader("Authorization", "Bearer my-secret-token")) {
2  return HttpResponse.unauthorized()
3}

parseUrlencodedForm(): Array<{ key: string; value: string }>

Parses the request body as application/x-www-form-urlencoded form data. Typically used for HTML form submissions via POST.

Returns: An array of key–value pairs representing form fields.

Example:

1const form = request.parseUrlencodedForm()
2for (const field of form) {
3  console.log(field.key, "=", field.value)
4}

If the body is:

username=thom&password=1234

then the output is:

username = thom password = 1234

parseMultiPartFormData(): Array<{ name: string | null; filename: string | null; headers: Record<string, string>; data: Data }>

Parses multipart/form-data requests, typically used for file uploads.

Returns: An array of form parts, where each element contains:

Property Type Description
name string | null The field name.
filename string | null The uploaded filename if the part is a file, otherwise null.
headers Record<string, string> The part’s headers.
data Data The raw content of the field or file.

Example:

1const parts = request.parseMultiPartFormData()
2for (const part of parts) {
3  if (part.filename) {
4    console.log("Uploaded file:", part.filename)
5    FileManager.writeAsDataSync(Path.join(Script.directory, part.filename), part.data)
6  } else {
7    console.log("Field:", part.name, "=", part.data.toRawString("utf-8"))
8  }
9}

Full Example

The example below shows how to read request data and respond accordingly:

1server.registerHandler("/upload", (req) => {
2  if (req.method === "POST") {
3    const parts = req.parseMultiPartFormData()
4    for (const part of parts) {
5      if (part.filename) {
6        console.log("Received file:", part.filename)
7      } else {
8        console.log("Field:", part.name)
9      }
10    }
11    return HttpResponse.ok(HttpResponseBody.text("Upload successful"))
12  } else {
13    return HttpResponse.badRequest(HttpResponseBody.text("POST required"))
14  }
15})