Skip to Content
How-To GuidesScalaHTTP Request and Response Parameter Mapping (Scala)

HTTP Request and Response Parameter Mapping (Scala)

Overview

When an agent is exposed over HTTP, Golem maps parts of each HTTP request to constructor and method parameters. This skill covers how path segments, query parameters, headers, and request bodies are mapped, which types are supported for each, and how return types map to HTTP responses.

Path Variables

Path variables {varName} in mount or endpoint paths map to parameters by name:

// Mount path variables → constructor parameters (from class Id) @agentDefinition(mount = "/api/tasks/{name}") trait TaskAgent extends BaseAgent { class Id(val name: String) // Endpoint path variables → method parameters @endpoint(method = "GET", path = "/items/{itemId}") def getItem(itemId: String): Future[Item] }

Remaining (catch-all) path variables capture everything after a prefix:

@endpoint(method = "GET", path = "/files/{*filePath}") def getFile(filePath: String): Future[String] // GET .../files/docs/readme.md → filePath = "docs/readme.md"

Catch-all variables can only appear as the last path segment and are not allowed in mount paths.

Query Parameters

Specified in the endpoint path using ?key={var} syntax:

@endpoint(method = "GET", path = "/search?q={query}&limit={n}") def search(query: String, n: Int): Future[String] // GET .../search?q=hello&limit=10

Header Variables

Map HTTP headers to parameters using the @header annotation on individual parameters:

import golem.runtime.annotations.header @endpoint(method = "POST", path = "/report") def submitReport( @header("X-Tenant") tenantId: String, data: String ): Future[String]

Supported Types for Path, Query, and Header Variables

Only these types can be used for parameters bound to path/query/header variables (the value is parsed from the URL/header string):

Scala TypeParsed From
StringUsed as-is
BooleanParsed from "true" / "false"
IntParsed as 32-bit signed integer
LongParsed as 64-bit signed integer
FloatParsed as 32-bit float
DoubleParsed as 64-bit float
Enum / sealed trait (unit cases only)Matched against known case names

For query parameters and headers only (not path variables), two additional wrapper types are supported:

Scala TypeBehavior
Option[T] (where T is a supported type above)Optional — absent query param or header produces None
List[T] / Array[T] (where T is a supported type above)Repeated query params or comma-separated header values

All other types (case classes, tuples, sealed traits with data, etc.) can only be used as body parameters.

POST Request Body Mapping

For POST/PUT/DELETE endpoints, method parameters not bound to path variables, query parameters, or headers are populated from the JSON request body:

@endpoint(method = "POST", path = "/items/{id}") def updateItem(id: String, name: String, count: Int): Future[Item] // POST .../items/123 // Body: { "name": "Widget", "count": 5 } // → id from path, name and count from body

Each unmapped parameter becomes a top-level field in the expected JSON body object. All custom types require a zio.blocks.schema.Schema instance.

⚠️ Important: The request body is always a JSON object with parameter names as keys — even when there is only a single body parameter. For example, an endpoint def decide(decision: String) expects {"decision": "approved"}, never a bare string like "approved". Sending a non-object JSON value or plain text will fail with REQUEST_JSON_BODY_PARSING_FAILED.

Return Type to HTTP Response Mapping

Return TypeHTTP StatusResponse Body
Future[Unit]204 No Contentempty
Future[T]200 OKJSON-serialized T
Future[Option[T]]200 OK if Some, 404 Not Found if NoneJSON T or empty

Data Type to JSON Mapping

Scala TypeJSON Representation
StringJSON string
Int, LongJSON number (integer)
Float, DoubleJSON number (float)
BooleanJSON boolean
Array[T], List[T]JSON array
case class (with Schema)JSON object (camelCase field names)
Option[T]value or null
sealed trait / enumJSON variant
TupleJSON array
Last updated on