AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::rest::FormUrlEncoded Struct Reference

#include <formCodec.hpp>

Collaboration diagram for anch::rest::FormUrlEncoded:

Static Public Member Functions

static std::string_view mediaType () noexcept
static anch::rest::FormLimits & limits (anch::codec::Side side)
static void serialize (const anch::rest::Form &form, std::ostream &out)
static void deserialize (anch::rest::Form &form, std::istream &input, const anch::codec::MediaType &contentType, anch::codec::Side side)
template<anch::rest::FormMapped T>
static void serialize (const T &value, std::ostream &out, anch::codec::Side side)
template<anch::rest::FormMapped T>
static void deserialize (T &value, std::istream &input, const anch::codec::MediaType &contentType, anch::codec::Side side)
static std::string encode (std::string_view value)

Detailed Description

application/x-www-form-urlencoded codec for Form (fields only).

Values are written with the WHATWG URL-encoded serializer (space is + ). On reading, + is a space, empty pairs are ignored and a pair without = is a field with an empty value. Content is UTF-8: another charset is refused.

Types with a FormMapper are read and written through a Form .

api.post("/login", [](anch::rest::Body<anch::rest::Form> form) {
std::string user = anch::rest::requiredFormField<std::string>(*form, "user"); // 400 when missing
std::optional<bool> remember = anch::rest::formField<bool>(*form, "remember"); // 400 when invalid
return login(user, remember.value_or(false));
})
.consumes<anch::rest::FormUrlEncoded>();
client.post("/login").body<anch::rest::FormUrlEncoded>(anch::rest::Form().add("user", "rex").add("password", pwd)).send();
Author
Vincent Lachenal
Since
0.2

Member Function Documentation

◆ deserialize() [1/2]

void anch::rest::FormUrlEncoded::deserialize ( anch::rest::Form & form,
std::istream & input,
const anch::codec::MediaType & contentType,
anch::codec::Side side )
static

Read form

Parameters
formthe form to fill
inputthe input stream
contentTypethe body media type
sidethe side which reads form
Exceptions
std::invalid_argumentwhen encoding is invalid or when charset is not UTF-8
ContentTooLargewhen body is over limits
Here is the call graph for this function:

◆ deserialize() [2/2]

void anch::rest::FormUrlEncoded::deserialize ( T & value,
std::istream & input,
const anch::codec::MediaType & contentType,
anch::codec::Side side )
static

Read value mapped with its FormMapper

Template Parameters
Tthe value type
Parameters
valuethe value to fill
inputthe input stream
contentTypethe body media type
sidethe side which reads value
Here is the call graph for this function:

◆ encode()

std::string anch::rest::FormUrlEncoded::encode ( std::string_view value)
static

Encode value with the WHATWG URL-encoded serializer: ASCII alphanumeric characters and *-._ are kept, space is + , other bytes are percent-encoded

Parameters
valuethe value
Returns
the encoded value
Here is the call graph for this function:

◆ limits()

anch::rest::FormLimits & anch::rest::FormUrlEncoded::limits ( anch::codec::Side side)
static

Get limits of a side, to change before handling requests. Default: 2 MiB body, 1000 fields.

Parameters
sidethe side
Returns
the limits
Here is the call graph for this function:

◆ mediaType()

std::string_view anch::rest::FormUrlEncoded::mediaType ( )
staticnoexcept

Media type

Returns
application/x-www-form-urlencoded

◆ serialize() [1/2]

void anch::rest::FormUrlEncoded::serialize ( const anch::rest::Form & form,
std::ostream & out )
static

Write form

Parameters
formthe form
outthe output stream
Exceptions
std::invalid_argumentwhen form has a file part
Here is the call graph for this function:

◆ serialize() [2/2]

void anch::rest::FormUrlEncoded::serialize ( const T & value,
std::ostream & out,
anch::codec::Side side )
static

Write value mapped with its FormMapper

Template Parameters
Tthe value type
Parameters
valuethe value
outthe output stream
sidethe side which writes value
Exceptions
std::invalid_argumentwhen form has a file part
Here is the call graph for this function:

The documentation for this struct was generated from the following file: