Skip to Content
How-To GuidesRustHTTP 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(), } }

Plain Text Request and Response Bodies

Use UnstructuredText from the SDK for raw text/plain payloads. Like UnstructuredBinary, a method using UnstructuredText may have only one body parameter and that parameter cannot be bound to a path/query/header/mount variable. The body is decoded as UTF-8.

use golem_rust::agentic::UnstructuredText; use golem_rust::AllowedLanguages; // Accepting any text/plain content #[endpoint(post = "/notes/{id}")] fn add_note(&self, id: String, body: UnstructuredText<String>) -> u64; // Restricting to specific language codes #[derive(AllowedLanguages, Clone, Debug)] pub enum Languages { #[code("en")] En, #[code("de")] De, } #[endpoint(post = "/translate/{id}")] fn translate(&self, id: String, body: UnstructuredText<Languages>) -> String; // Returning text/plain #[endpoint(get = "/notes/{id}")] fn get_note(&self, id: String) -> UnstructuredText<String>;

HTTP-level rules:

  • The request must have either no Content-Type, text/plain, or text/plain; charset=utf-8 (case-insensitive). Any other content type is rejected with 415 Unsupported Media Type.
  • Content-Language is always optional, even when AllowedLanguages is restricted. If present, it must be a single value (multi-valued or comma-separated headers are rejected with 400 Bad Request).
  • When restricted, the supplied Content-Language is matched case-insensitively against the allowed list; otherwise 415 Unsupported Media Type.
  • A non-UTF-8 request body is rejected with 400 Bad Request.
  • Content-Language cannot also be bound as an endpoint header parameter when the body is UnstructuredText — that header is reserved for declaring the body language.

In the implementation:

fn add_note(&self, _id: String, body: UnstructuredText<String>) -> u64 { match body { UnstructuredText::Url(_) => 0, UnstructuredText::Text { data, .. } => data.len() as u64, } } fn get_note(&self, _id: String) -> UnstructuredText<String> { UnstructuredText::Text { data: "hello".to_string(), language_code: Some("en".to_string()), } }

The response is sent as Content-Type: text/plain; charset=utf-8. If the returned UnstructuredText::Text has a language_code, it is forwarded as the Content-Language response header.

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
UnstructuredText<M>200 OKtext/plain; charset=utf-8 (+ optional Content-Language)

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
Last updated on