req

This model gives access to the request variables from within a template.

Sometimes you need direct access to request variables in your template. The m_req model is meant for this. It exposes some values from the Cowmachine request record.

Fetching a single value

You can fetch individual values by key, for example:

{{ m.req.host|escape }}

Viewing all request variables

Use the print tag to get a complete overview of all request variables:

{% print m.req|make_list %}

This will show something like:

[{method,<<"GET">>},
 {version,{1,1}},
 {peer,<<"127.0.0.1">>},
 {is_ssl,false},
 {host,<<"mysite.test">>},
 {raw_path,<<"/en/page/1234?foo=bar">>},
 {path,<<"/en/page/1234">>},
 {qs,[{<<"foo">>,<<"bar">>}]},
 {referrer,<<"http://mysite.test:8000/">>},
 {user_agent,<<"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4">>},
 {is_crawler,false},
 {req_id,525158920},
 {headers,[{<<"accept">>,
            <<"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8">>},
           {<<"accept-encoding">>,<<"gzip, deflate">>},
           {<<"accept-language">>,<<"en-us">>},
           {<<"cache-control">>,<<"max-age=0">>},
           {<<"connection">>,<<"keep-alive">>},
           {<<"cookie">>,
            "z_logon=; z_sid=LopWHBmHXCs94virnboZhBHLKV6m1Cga; z_ua=c%3Ddesktop%26u%3D1%26t%3D1%26w%3D1920%26h%3D1200"},
           {<<"dnt">>,<<"1">>},
           {<<"host">>,<<"mysite.test:8000"},
           {<<"referer">>,<<"http://mysite.test:8000/">>},
           {<<"user-agent">>,
            <<"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4">>}]},
 {timezone,<<"UTC">>},
 {language,en}]

Please note that all values are raw and not escaped, take care to escape the values before you use them in your templates, using the escape filter.

The make_list filter is used to force the evaluation of the model; otherwise it would just print {m,req,undefined}.

Available Model API Paths

MethodPath patternDescription
get/+key/...Return request/context value for key +key (must map to an existing atom key); returns unknown_key for unknown key names.
get/Return default request overview list from values/1 (method, version, peer, is_ssl, host, port, raw_path, path, qs, referrer, user_agent, is_crawler, headers, timezone, language). No further lookups.

/+name marks a variable path segment. A trailing /... means extra path segments are accepted for further lookups.

Known Keys

The following keys are recognized by m.req.+key:

KeyDescription
methodHTTP method as binary, for example <<"GET">>.
versionHTTP protocol version tuple, for example {1,1}.
peerRemote client IP as binary text.
peer_ipRemote client IP as tuple form.
is_sslWhether request is HTTPS (true/false).
schemeRequest scheme (<<"http">> / <<"https">>).
hostRequest host header (without path).
portEffective request port (proxy-aware; site config port when not proxied).
raw_pathRaw request path including query string.
pathRequest path without query string.
qsParsed query arguments list.
headersAll request headers as {Name, Value} pairs.
refererReferer request header value.
referrerAlias of referer.
user_agentUser-Agent header value (truncated to max 500 chars).
is_crawlerWhether User-Agent is detected as crawler.
siteCurrent Zotonic site name.
timezoneCurrent request/context timezone.
languageCurrent request/context language.
is_http_requestContext flag indicating HTTP request processing context.
session_idCurrent session id when available, else undefined.
csp_nonceCSP nonce for the current response/context.

Edit on GitHub

hierarchy Models sysconfig

Referred by

Global template variables

These variables are always available for rendering in templates.