# HTTP context mapping The `map` directive family is used to map HTTP request parts to operation call input properties. [Features](../features/map.feature). ## TL;DR ```yaml exposition: /:group: languages: [en, fr] # supported languages GET: map:authority: hostname # request authority (e.g., hostname) map:language: lang # requested language map:headers: # raw header values token: x-access-token map:buffer: body # unparsed request body map:segments: # route parameters group: group map:claims: # Bearer token claims address: email verified: email_verified map:ip: ip # the client address map:country: country # the client's country endpoint: observe ``` The operation input type must be an object. If the input already contains the specified keys, they will be overwritten. ## Authority The `map:authority` directive maps the [authority identifier](authorities.md) to an operation call input property specified by the directive value. ### Language The `map:language` mapping sets the [most matching](https://github.com/jshttp/negotiator) language code based on the `accept-language` request header and a list of supported languages defined by the `map:languages` directive, and also adds `accept-language` to the `Vary` HTTP response header value. If none of the supported languages match, the first supported language is used. > `map:languages` has a shorthand form: `languages: [en, fr]`. ## Header values The `map:headers` directive maps the values of HTTP request headers to operation call input properties. The value of the directive is a map where keys are the names of the input properties and values are the names of the HTTP request headers. A header the request does not have is a property the input does not have, whatever the body says: what a mapping fills is not the caller's to send. The names of these headers are then included in the `Vary` HTTP response header. A page on another origin may send any of them ([CORS](protocol.md#cors)). [Multiple header fields](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2) are combined as a comma-separated list. ## Raw body The `map:buffer` directive maps the unparsed request body to an operation call input property. The directive value is the name of the input property. ```yaml /webhook: POST: map:buffer: body map:headers: signature: x-signature endpoint: verify ``` The body is read from the request stream as raw bytes and delivered as a UTF-8 string. Body parsing is bypassed, so the route does not depend on a supported `Content-Type` or format decoding. The request stream is consumed; a route with `map:buffer` cannot also receive a parsed body from the same request. ## Stream The `map:stream` directive hands the request body to the operation as a stream, instead of reading it. The operation names the input property that carries one — see [streamed input](/documentation/streams.md) — and the directive names the same property: ```yaml /videos/:id: POST: map:segments: id: id map:stream: source endpoint: transcode ``` The property is filled with `{ type, accept, stream }`: the request's content type, the media type to answer with, and the body. Body parsing is bypassed and the request stream is consumed, so a route with `map:stream` cannot also receive a parsed body, and cannot carry `map:buffer`. What a route takes and what it answers are stated beside it: ```yaml map:stream: property: source accept: [video/quicktime, video/mp4] produces: [video/mp4, image/jpeg] limit: 4GiB ``` - `accept` is what a client may send. A request of another media type is rejected with `415`. Absent, any media type is accepted. - `produces` is what this route may answer. The request's `accept` is resolved against it before the call, and the operation is handed that one media type as `accept`; a client asking for anything else is answered `406`, and the operation does not run. **Everything it names is one answer in another encoding** — a picture as `image/webp` or `image/jpeg`, a render as `application/pdf` or `image/png` — because what a client accepts chooses an encoding and not an operation. A route that would answer two different things answers them at two paths. Where a client names several of them, the order it named them in decides; where it names none, or takes anything, the route's own order does. - `limit` is the largest body the route takes, as a number of bytes or with a unit — see [stream size limit](octets.md#stream-size-limit). Past it, `413`. The default is `64MiB`. A byte stream the operation answers is served under the media type it was handed. **A route that states `produces` answers bytes**: a value answered there is `422`. A streamed call takes a connection of its own and is neither queued, retried nor balanced, so map a stream only where a body has to flow through an operation. A body that is to be kept is stored by [`octets:put`](octets.md), which calls components afterwards with a reference to it; a body small enough to be a value is a value. See [what it costs](/documentation/streams.md#what-it-costs). ## Route parameters The `map:segments` directive maps the values of route parameters to operation call input properties. The value of the directive is a map where keys are the names of the input properties, and values are the names of the route parameters. Parameter name may be prefixed with `~` to indicate that the parameter should not be available to the remaining directives or used as criteria in the operation call. ```yaml /:id/:tag: POST: map:segments: id: id tag: ~tag endpoint: create ``` Wildcard routes are supported by using `**` as the parameter name. ```yaml /path/to/**: GET: map:segments: path: '**' endpoint: observe ``` ## Instance The `map:instance` directive names the route parameter that carries the process a [stateful operation](/documentation/stateful.md) is called on. ```yaml /streams/:instance/:id: GET: map:instance: instance endpoint: watch ``` The parameter is taken out of the input and out of the criteria. A stateful operation behind a route without `map:instance` answers `400`. A name no process holds answers `404`, and a call the process holding it leaves unanswered past its timeout answers `504`. ## Client address and country The `map:ip` and `map:country` directives map the client address and country, as [`exposition.ip` and `exposition.country`](ip.md) read them, to an operation call input property. ```yaml POST: map:ip: address map:country: country endpoint: record ``` The property is set on every call, as `null` where the request has none, so a body that names it is overwritten rather than believed. ## Bearer token claims The `map:claims` directive maps the values of the [token claims](https://datatracker.ietf.org/doc/html/rfc7519#section-4). The value of the directive is a map where keys are the names of the input properties and values are the names of the claims. If the claim is not present in the token or the request is not authenticated using the [`Bearer` scheme](identity.md#bearer-scheme), the input properties are not set. ## Introspection A property a mapping fills is not the caller's to send, so [`OPTIONS`](introspection.md) takes it out of the input. `map:headers` states it under `headers`, naming the header it is read from, and `map:segments` under `route`, named as the property rather than as the segment, and `map:instance` under `route` as `instance`. The rest — `map:claims`, `map:authority`, `map:language`, `map:buffer` — come from the request itself, and a caller has nowhere to put one.