Skip to content

HTTP RFC

This page defines the core mapping rules from XIDL interface to HTTP APIs, including:

  • Method and route mapping
  • Parameter source resolution
  • Request/response encoding
  • Attribute mapping

This page does not discuss OpenAPI generation details, nor does it serve as a tutorial.

This RFC mainly covers:

  • HTTP verb and path annotations
  • Route template resolution
  • Parameter source priority
  • Request and response body shaping
  • HTTP semantics of in / out / inout
  • @get(path = "...")
  • @post(path = "...")
  • @put(path = "...")
  • @patch(path = "...")
  • @delete(path = "...")
  • @head(path = "...")
  • @options(path = "...")
  • @path @rename("...")
  • @request("json" | "urlencoded" | "msgpack")
  • @response("json" | "urlencoded" | "msgpack")
  • @cors(...)
  • @deprecated
  • @path
  • @query
  • @body
  • @header
  • @cookie
  • @flatten
  • @optional

A source annotation may contain one string name. These parameter declarations are equivalent:

@header @rename("Idempotency-Key") string key
@header(name = "Idempotency-Key") string key
@header("Idempotency-Key") string key

The same forms apply to @path, @query, and @cookie. A parameter-level @path(path = "id") is also accepted. The annotation selects the HTTP location; its string argument selects the transmitted name. The generated function argument keeps the source identifier, such as key above.

The location annotation is still required when a renamed parameter does not match the normal source-based inference rules. @rename alone does not select the path or query location:

@get(path = "/messages/{message_id}{?expand}")
string read(@path @rename("message_id") string id,
@query(name = "expand") @optional string fields);

If both the source annotation and @rename provide a name, @rename takes precedence regardless of annotation order. More than one string argument, unknown argument keys, non-string values, conflicting locations and conflicting names for repeated source annotations are rejected.

Request names must be unique within each HTTP location. Header comparisons ignore ASCII case; query and cookie comparisons preserve case. Output headers may have repeated names. Header and cookie names must be valid HTTP tokens.

Exception header and cookie members use the same name rules. Exception JSON body members retain the data model’s @rename and @rename_all rules.

Rules:

  • A method can only have one HTTP verb annotation.
  • When no explicit verb is provided, it defaults to @post.
  • The path can come from the path attribute in the verb annotation or from a method-level @path(...) annotation.
  • Multiple path declarations are merged, normalized, and de-duplicated.

Supported formats:

  • /users/{id}
  • /files/{*path}
  • /users/{id}{?lang,region}

Semantics:

  • {name}: Normal path variable.
  • {*name}: Capture trailing multi-segment paths.
  • {?a,b}: Declare query parameter names.

Before conflict detection and actual binding, all routes are normalized:

  1. Remove leading and trailing whitespace.
  2. Ensure it starts with /.
  3. Merge duplicate / characters.
  4. Remove trailing / characters.
  5. Maintain case; do not automatically rewrite segment content.

One interface is one route table, so a duplicate binding is rejected during projection.

Rules:

  • Within one interface, a normalized path can be bound at most once per HTTP verb.
  • Identical paths may appear in different interfaces; each interface is mounted by the generated entry point separately.
  • Use an @http union when one route must serve several representations.

The compiler reports the interface, the method and path, and both operations:

duplicate HTTP route binding in interface 'DirectoryApi': GET /users/{id} (operations: get_user, fetch_user)

Because the check runs while the contract is projected, every target rejects the same contract, and a versioned contract is checked once per projected API version.

When a method has no explicit path:

  1. Use /{method_name} as the base path.
  2. Append all Path parameters in the order they are declared.
  3. Query parameters do not participate in route template generation.

Example:

void findUser(@path uint32 id, @query string locale);

Results in:

POST /findUser/{id}

A parameter may declare one explicit source: @path, @query, @body, @header, or @cookie. Conflicting sources are rejected; the compiler does not choose one by priority. With no explicit source, it infers the binding from the route template, HTTP method, and parameter direction.

Wire naming is independent of source selection: @rename / @name overrides a name supplied by a source annotation. See the HTTP annotation reference.

Default inference practices:

  • Unlabeled request parameters for GET / DELETE / HEAD / OPTIONS usually go to Query.
  • Unlabeled request parameters for POST / PUT / PATCH usually go to Body.

The body parameters of one operation decide the JSON request body:

  • Exactly one @body parameter: the request body is that parameter’s type as-is. @body Item item sends an Item — no wrapper object.

    interface Store {
    @post(path = "/items")
    Item create(@body Item item);
    };
  • Several @body parameters: the request body is an object with one property per body parameter, keyed by its canonical wire name.

    interface Store {
    @post(path = "/items")
    Item create(@body Item item, @body Tag tag);
    };
    // request body: {"item": {...}, "tag": {...}}

@flatten on a @body parameter does not change the wire shape; it is a generated-model composition hint and is only legal as the operation’s single body parameter.

Direction rules:

  • in: Participates only on the request side.
  • out: Participates only on the response side.
  • inout: Participates on both sides.

Common interpretation in HTTP mapping:

  • Request-side parameters determine the path, query, headers, cookies, and body.
  • The response side is determined by the return value and out / inout parameters.

@head has additional constraints:

  • The return type must be void.
  • It cannot have response body parameters; response header and cookie parameters are allowed.
  • The response does not contain a body.

@request and @response respectively define:

  • Request body decoding format.
  • Response body encoding format.

The format tokens map to concrete MIME types:

  • json -> application/json
  • urlencoded -> application/x-www-form-urlencoded
  • msgpack -> application/msgpack

text/plain and application/octet-stream are derived automatically and must not be declared with these annotations: primitive values serialize as text/plain, and sequence<octet> stream payloads are raw byte streams (application/octet-stream).

Serialization rules:

  • By default, primitive types (such as string, int32, bool, etc.) are encoded directly as the text representation of their raw values.
  • Composite types (such as struct, union, sequence, map) default to application/json.

Interface-level annotations serve as defaults, which method-level annotations override.

The @cors annotation is used to declare Cross-Origin Resource Sharing (CORS) policies, targeting interfaces or methods.

Think of it as “attaching a cross-origin response policy to this set of HTTP routes.”

Supported basic forms:

  • @cors: Unconditionally enable CORS.
  • @cors("https://app.example.com", "https://admin.example.com"): Accept only these origins.

Example:

@cors
interface UserApi {
@get(path = "/users/{id}")
string getUser(@path @rename("id") string id);
@cors("https://admin.example.com", "https://console.example.com")
@post(path = "/users")
void createUser(string name);
};

Where:

  • @cors means the interface or method accepts cross-origin access from any origin.
  • @cors("...", "...") means the interface or method only accepts the listed Origins.
  • Multiple string parameters represent an origin allowlist.

Inheritance and overriding rules:

  • Interface-level @cors serves as the default policy.
  • Method-level @cors overrides the interface-level default policy.
  • If a method does not declare @cors, it inherits the interface-level configuration.

In the example above:

  • getUser inherits the interface-level @cors, thus unconditionally enabling CORS.
  • createUser uses its own method-level @cors("...", "...").
  • The final policy for createUser follows the method declaration, accepting only the listed origins.

Runtime semantic recommendations:

  • Routes matching @cors should return the corresponding Access-Control-Allow-Origin.
  • When a browser initiates an OPTIONS preflight request, the response should be calculated based on the same policy.
  • The difference between @cors and @cors("...", "...") is mainly in the set of allowed origins.

These rules are HTTP boundary behaviors and do not change XIDL method signatures or participate in business parameter binding.

In HTTP scenarios:

  • Optional Query parameters can be omitted.
  • Optional Body fields can be omitted.
  • Path parameters cannot be optional.

This determines how field requirement is understood in OpenAPI and runtime validation.

Attributes are mapped to implicit operations, which then follow normal HTTP mapping rules.

In other words, attributes themselves are not independent special channels; they enter HTTP semantics through the generated access operations.

If you are implementing a generator or runtime:

  • Perform strict validation first; do not silently tolerate conflicting annotations.
  • Route normalization, parameter source resolution, and response shaping should be stable and reproducible.
  • Tutorial pages can be simplified, but RFC rules must not be ambiguous.