Skip to content
spaceport / docs
The Spaceport handbook

Routing — API Reference

This document covers the HTTP-specific configuration and utilities that shape how Spaceport handles requests. For the @Alert annotation, HttpResult methods, and event string syntax, see the Alerts API Reference — routing handlers are alert handlers, and their full API lives there.


HttpContext Properties

Every HTTP alert handler receives an HttpResult whose context property is an HttpContext. This table summarizes what's available:

Property Type Description
request HttpServletRequest The raw Jetty servlet request object
response HttpServletResponse The raw Jetty servlet response object
method String HTTP method in uppercase: GET, POST, PUT, DELETE, etc.
target String The request path (e.g., /users/123). Does not include query string.
time Long Timestamp when the request was received (System.currentTimeMillis())
cookies Map<String, String> Request cookies, keyed by cookie name
headers Map<String, String> Request headers, keyed by header name
data Map Parsed request data from all sources (see Request Data Parsing). Repeated query/form parameters (e.g. ?tag=a&tag=b) are collected into a List<String> for that key. Use context.list(name) when you want a list regardless of whether the original value arrived as a single string or as a list.
dock Cargo Session-scoped reactive data container tied to the client
client Client The client/session object for this request
resultType Class Set to HttpResult — used internally to instantiate the correct Result subclass

Usage:

@Alert('on /profile hit')
static _profile(HttpResult r) {
    def path   = r.context.target       // "/profile"
    def method = r.context.method       // "GET"
    def token  = r.context.headers['Authorization']
    def userId = r.context.data.userId  // from query string or body
    def client = r.context.client       // Client object
}

The data map is bidirectional: you read request data from it, but you can also write to it to pass data forward to templates or downstream handlers.

@Alert(value = 'on /profile hit', priority = 10)
static _loadProfile(HttpResult r) {
    r.context.data.user = Document.getIfExists(r.context.data.userId, 'users')
}

@Alert('on /profile hit')
static _renderProfile(HttpResult r) {
    // r.context.data.user is available here, and in Launchpad templates
}

Request Data Parsing

Spaceport automatically parses the request body based on the Content-Type header and merges the result into r.context.data. Query string parameters are always parsed.

Content-Type Parsing Behavior
application/json JSON body parsed into a Map via JsonSlurper. Nested structures preserved.
multipart/form-data Each part extracted. File parts stored as [name: String, data: byte[]] maps (or a List of maps when multiple files share a field name). Non-file parts stored as strings. Maximum request size: 50 MB.
application/x-www-form-urlencoded Standard form key-value pairs parsed into the data map.
(query string) Query parameters parsed from the URL and merged into the data map. Always parsed, regardless of Content-Type. A missing Content-Type header is handled safely.
(other / none) No body parsing. Only query string parameters are available.

Precedence: If the same key appears in both the query string and the body, the body value takes precedence.

Repeated parameters

A parameter that appears more than once in the query string or form body (?tag=red&tag=blue) lands in r.context.data as a List<String>. To avoid having to branch on "is this a String or a List?", use r.context.list(name):

List tags = r.context.list('tag')   // always a List<String>, even when one or zero values were sent

context.list(name) returns an empty list when the key is absent or null, the existing list when the value is already a List, or a single-element list wrapping the value otherwise.

File Uploads

For multipart uploads, file parts arrive as maps with name (the submitted filename) and data (byte[] file content). If multiple files are uploaded under the same field name, r.context.data.<field> is a List of those maps (use r.context.list('file') to handle single and multiple files uniformly):

@Alert('on /upload POST')
static _upload(HttpResult r) {
    def file     = r.context.data.file  // Map: [name: 'photo.jpg', data: byte[]]
    def fileName = file.name
    byte[] bytes = file.data
    // Process the uploaded file...
}

Manifest Configuration

Routing behavior is configured in the Spaceport manifest file. All settings have sensible defaults.

Server Binding

Key Type Default Description
host.address String 127.0.0.1 IP address the server binds to. Use 0.0.0.0 to listen on all interfaces.
host.port Integer 10000 Port the server listens on.
host:
  address: 0.0.0.0
  port: 8080

HTTP Method Whitelist

Key Type Default Description
http.methods.allow List<String> ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'] Allowed HTTP methods. Requests with methods not in this list receive a 405 Method Not Allowed response.
http:
  methods:
    allow:
      - GET
      - POST
      - PUT
      - DELETE
      - PATCH

OPTIONS and HEAD requests are handled automatically regardless of this setting (see Auto-Processed Methods).

Auto-Processed Methods

Key Type Default Description
http.auto process options Boolean true When true, OPTIONS requests are answered automatically with CORS headers. When false, OPTIONS requests pass through to your alert handlers.

HEAD requests are always auto-processed — they run the normal handler pipeline but suppress the response body.

Cache Control

Key Type Default Description
http.cache control String 'no-cache' Value of the Cache-Control response header.
http.pragma String 'no-cache' Value of the Pragma response header.
http.expires String '0' Value of the Expires response header.
http:
  cache control: no-store, must-revalidate
  pragma: no-cache
  expires: '0'

These headers are set on every HTTP response before your handlers execute.

CORS Configuration

Key Type Default Description
http.allow credentials Boolean true Sets Access-Control-Allow-Credentials header.
http.allow headers String '*' Value of Access-Control-Allow-Headers.
http:
  allow credentials: true
  allow headers: Content-Type, Authorization, X-Custom

CORS behavior:

Key Type Default Description
http.spaceport cookie expiration Integer 5184000 (60 days) Max-Age in seconds for the spaceport-uuid client identification cookie.
http:
  spaceport cookie expiration: 7776000  # 90 days

The spaceport-uuid cookie is set automatically when a browser first visits, with HttpOnly, SameSite=Lax, and (in production) Secure attributes. It identifies the client across requests and ties them to a Client session. See Routing Internals for details on client identification.

Static Assets

Key Type Default Description
static assets.directory String 'public' Directory (relative to project root) containing static files
static assets.url String '/public' URL prefix for serving static files
static assets:
  directory: public
  url: /public

Static assets are served directly by Jetty's resource handler, bypassing the alert pipeline entirely. They are handled before HTTP requests reach the HttpRequestHandler.

Default Content Type

The default response Content-Type is text/html;charset=UTF-8. This is applied to all responses unless explicitly changed by a handler (via r.setContentType() or by writing a Map/List, which sets JSON content type automatically).


Outbound HTTP Utility

Spaceport provides a built-in HTTP utility class for making outbound HTTP requests from your server-side code. For full documentation, see the HTTP Client Reference.

import spaceport.communications.http.HTTP

Quick example:

def response = HTTP.get('https://api.example.com/users', [
    queryParams: [page: '2', limit: '10'],
    headers: ['Accept': 'application/json']
])

if (response.statusCode == 200) {
    def users = new groovy.json.JsonSlurper().parseText(response.body)
}

HttpResult Quick Reference

The HttpResult class (fully documented in Alerts API Reference) provides the following categories of methods:

Category Key Methods
Response writing writeToClient(String), writeToClient(Map), writeToClient(List), writeToClient(File)
Status & headers setStatus(Integer), setContentType(String), addResponseHeader(String, def), setResponseHeader(String, def)
Redirects setRedirectUrl(String)
Cookies addResponseCookie(String, String), addSecureResponseCookie(String, String), removeResponseCookie(String)
File downloads markAsAttachment(String)
Auth authorize(Closure), ensure(Closure), getClient()
Flow control r.cancelled = true (stop handler chain), r.called (check if any non-passive handler claimed the request)

See Alerts API Reference for full method signatures, parameter types, and examples.

Make the docs better.Edit this page on GitHub