Macro anterofit::fields [−][src]
macro_rules! fields { ($($key:expr $(=> $val:expr)*),*) => { ... }; ($($key:expr $(=> $val:expr)*),*,) => { ... }; }
Serialize a series of fields as the request body (form-encode them).
Each field can be a key-value pair, or a single identifier. The key (field name) should be a
string literal, and the value can be anything that is Display
.
For a single identifier, the identifier will be stringified for the field name, and its value will become the field value:
service! { pub trait RegisterService { fn register(&self, username: &str, password: &str) { POST("/register"); fields! { "username" => username, // Equivalent to "password" => password password } } } }
By default, this will serialize to a www-form-urlencoded
body.
However, if you use the path!()
or stream!()
macros as a value expression,
it will transform the request to a multipart/form-data
request.
use std::path::Path; service! { pub trait UploadService { fn upload_file(&self, file: &Path) { POST("/upload"); fields! { "file" => path!(file), } } } }
In some server stacks (e.g. PHP), these would be called POST
parameters.
Overwrites Body
Setting a new body will overwrite any previous body on the request.
Disallowed verbs: GET, DELETE
GET
and DELETE
requests are generally not expected to have bodies. As an
anti-footgun, Anterofit does not allow bodies on these requests by default.
Wrap this invocation in force_body!()
if you want to set a body anyways.
See net::method::TakesBody
for more details.