leptus_req module

Table of Contents

leptus_req module

Please keep it in mind that Req is a Pid that should be used as the first argument in leptus_req functions.

All the functions given below are accessible through the leptus_req module, which are supposed to work with the Req object.

Types

Req = pid()

Functions

param/2

Returns a parameter that is bound to the route.

param(Req, Name :: atom()) -> binary() | undefined

%% e.g.
%% route: /items/:id
%% requested uri: /items/1863
param(Req, id) -> <<"1863">>

params/1

Returns parameters that are bound to the route.

params(Req) -> [{atom(), binary()}]

%% e.g.
params(Req) -> [{id, <<"1863">>}]

qs/1

Returns query strings.

qs(Req) -> binary()

%% e.g.
%% uri: /items/?limit=50
qs(Req) -> <<"limit=50">>

qs_val/2

Returns the given query string value.

qs_val(Req, Field :: binary()) -> binary() | undefined

%% e.g.
qs_val(Req, <<"limit">>) -> <<"50">>

uri/1

Returns the requested URI.

uri(Req) -> binary()

%% e.g.
uri(Req) -> <<"/items/?limit=50">>

version/1

Returns HTTP version.

version(Req) -> 'HTTP/1.1' | 'HTTP/1.0'

%% e.g.
version(Req) -> 'HTTP/1.1'

method/1

Returns used HTTP method.

method(Req) -> binary()

%% e.g.
method(Req) -> <<"DELETE">>

body/1

Returns received body (decoding might apply to it).

body(Req) -> binary() | json_term()

%% e.g.
body(Req) -> <<"foo=bar">>

%% when content-type is set to applicaation/json or application/msgpack
body(Req) -> [{<<"function">>, <<"body/1">>}]

body_raw/1

Returns raw body.

body_raw(Req) -> binary()

%% e.g.
body_raw(Req) -> <<"{\"function\": \"body/1\"}">>

body_qs/1

Returns body but in query string format.

body_qs(Req) -> [{binary(), binary() | true}]

%% e.g.
body_qs(Req) -> [{<<"foo">>, <<"bar">>}]

header/2

Returns the given header value.

header(Req, binary()) -> binary() | undefined

%% e.g.
header(Req, <<"content-type">>) -> <<"application/x-www-form-urlencoded">>

header/3

Returns the given header value or the default value if the header does not exist.

header(Req, binary(), Default) -> binary() | Default

%% e.g.
header(Req, <<"content-type">>, undefined) -> undefined

parse_header/2

Parses the given header.

parse_header(Req, binary()) -> any() | undefined | {error, any()}

%% e.g.
parse_header(Req, <<"content-type">>) -> {<<"application">>, <<"json">>, []}

auth/2

Checks for the given authorization method.

NOTE: basic authentication is only supported at the moment.

auth(Req, basic) -> {binary(), binary()} | <<>> | error

%% e.g.
auth(Req, basic) -> {<<"username">>, <<"p4ssw0rd">>}

peer/1

Returns the IP address and the port number of the remote host.

peer(Req) -> {inet:ip_address(), inet:port_number()}

%% e.g.
peer(Req) -> {{127, 0, 0, 1}, 3846}

get_req/1

Returns the Cowboy Req object.

get_req(Req) -> cowboy_req:req()

set_req/2

Sets a new Cowboy Req object as the state of leptus_req so that leptus_req functions will use the new Req object.

set_req(Req, CowboyReq) -> ok

Author: Sina Samavati

Created: 2026-04-17 Fri 11:09

Validate