Welcome to the new Golem Cloud Docs! 👋
HTTP Request and Response Parameter Mapping (Rust)

HTTP Request and Response Parameter Mapping (Rust)

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 {var_name} in mount or endpoint paths map to parameters by name:

// Mount path variables → constructor parameters
#[agent_definition(mount = "/api/tasks/{task_name}")]
pub trait TaskAgent {
    fn new(task_name: String) -> Self;
 
    // Endpoint path variables → method parameters
    #[endpoint(get = "/items/{item_id}")]
    fn get_item(&self, item_id: String) -> Item;
}

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

#[endpoint(get = "/files/{*path}")]
fn get_file(&self, path: String) -> FileContent;
// GET .../files/docs/readme.md → path = "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(get = "/search?q={query}&limit={max_results}")]
fn search(&self, query: String, max_results: u64) -> Vec<SearchResult>;
// GET .../search?q=hello&limit=10

Header Variables

Map HTTP headers to parameters using the headers(...) block on #[endpoint]:

#[endpoint(
    get = "/data",
    headers("X-Request-Id" = "request_id", "Authorization" = "token")
)]
fn get_data(&self, request_id: String, token: String) -> Data;

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):

Rust TypeParsed From
StringUsed as-is
charSingle character
boolParsed from "true" / "false"
u8, u16, u32, u64Parsed as unsigned integer
i8, i16, i32, i64Parsed as signed integer
f32, f64Parsed as floating-point number
Enum (unit variants only)Matched against known case names

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

Rust TypeBehavior
Option<T> (where T is a supported type above)Optional — absent query param or header produces None
Vec<T> (where T is a supported type above)Repeated query params or comma-separated header values

All other types (structs, tuples, enums with data, HashMap, 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(post = "/items/{id}")]
fn update_item(&mut self, id: String, name: String, count: u64) -> 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 must derive Schema.

⚠️ 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 fn decide(&mut self, 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.

Binary Request and Response Bodies

Use UnstructuredBinary from the SDK for raw binary payloads:

use golem_rust::agentic::UnstructuredBinary;
use golem_rust::AllowedMimeTypes;
 
// Accepting any binary content type
#[endpoint(post = "/upload/{bucket}")]
fn upload(&self, bucket: String, payload: UnstructuredBinary<String>) -> i64;
 
// Restricting to specific MIME types
#[derive(AllowedMimeTypes, Clone, Debug)]
pub enum ImageTypes {
    #[mime_type("image/gif")]
    ImageGif,
    #[mime_type("image/png")]
    ImagePng,
}
 
#[endpoint(post = "/upload-image/{bucket}")]
fn upload_image(&self, bucket: String, payload: UnstructuredBinary<ImageTypes>) -> i64;
 
// Returning binary data
#[endpoint(get = "/download")]
fn download(&self) -> UnstructuredBinary<String>;

In the implementation:

fn upload(&self, _bucket: String, payload: UnstructuredBinary<String>) -> i64 {
    match payload {
        UnstructuredBinary::Url(_) => -1,
        UnstructuredBinary::Inline { data, .. } => data.len() as i64,
    }
}
 
fn download(&self) -> UnstructuredBinary<String> {
    UnstructuredBinary::Inline {
        data: vec![1, 2, 3, 4],
        mime_type: "application/octet-stream".to_string(),
    }
}

Return Type to HTTP Response Mapping

Return TypeHTTP StatusResponse Body
() (unit / no return)204 No Contentempty
T (any type)200 OKJSON-serialized T
Option<T>200 OK if Some, 404 Not Found if NoneJSON T or empty
Result<T, E>200 OK if Ok, 500 Internal Server Error if ErrJSON T or JSON E
Result<(), E>204 No Content if Ok, 500 if Errempty or JSON E
Result<T, ()>200 OK if Ok, 500 if ErrJSON T or empty
UnstructuredBinary<M>200 OKRaw binary with Content-Type

Data Type to JSON Mapping

Rust TypeJSON Representation
StringJSON string
u8u64, i8i64JSON number (integer)
f32, f64JSON number (float)
boolJSON boolean
Vec<T>JSON array
Struct (with Schema)JSON object (camelCase field names)
Option<T>value or null
Result<T, E>value (see response mapping above)
Enum (unit variants)JSON string
Enum (with data)JSON object with tag
HashMap<K, V>JSON array of [key, value] tuples