pub struct Data { /* fields omitted */ }
Type representing the data in the body of an incoming request.
This type is the only means by which the body of a request can be retrieved.
This type is not usually used directly. Instead, types that implement
FromData are used via code generation by
specifying the data = "<param>"
route parameter as follows:
#[post("/submit", data = "<var>")]
fn submit(var: T) -> ... { ... }
Above, T
can be any type that implements FromData
. Note that Data
itself implements FromData
.
Data may be read from a Data
object by calling either the
open or peek methods.
The open
method consumes the Data
object and returns the raw data
stream. The Data
object is consumed for safety reasons: consuming the
object ensures that holding a Data
object means that all of the data is
available for reading.
The peek
method returns a slice containing at most 512 bytes of buffered
body data. This enables partially or fully reading from a Data
object
without consuming the Data
object.
Returns the raw data stream.
The stream contains all of the data in the body of the request,
including that in the peek
buffer. The method consumes the Data
instance. This ensures that a Data
type always represents all of
the data in a request.
use rocket::Data;
fn handler(data: Data) {
let stream = data.open();
}
Retrieve the peek
buffer.
The peek buffer contains at most 512 bytes of the body of the request.
The actual size of the returned buffer varies by web request. The
peek_complete
can be used to determine if
this buffer contains all of the data in the body of the request.
use rocket::Data;
fn handler(data: Data) {
let peek = data.peek();
}
Returns true if the peek
buffer contains all of the data in the body
of the request. Returns false
if it does not or if it is not known if
it does.
use rocket::Data;
fn handler(data: Data) {
if data.peek_complete() {
println!("All of the data: {:?}", data.peek());
}
}
A helper method to write the body of the request to any Write
type.
This method is identical to io::copy(&mut data.open(), writer)
.
use std::io;
use rocket::Data;
fn handler(mut data: Data) -> io::Result<String> {
data.stream_to(&mut io::stdout())
.map(|n| format!("Wrote {} bytes.", n))
}
A helper method to write the body of the request to a file at the path
determined by path
.
This method is identical to
io::copy(&mut self.open(), &mut File::create(path)?)
.
use std::io;
use rocket::Data;
fn handler(mut data: Data) -> io::Result<String> {
data.stream_to_file("/static/file")
.map(|n| format!("Wrote {} bytes to /static/file", n))
}
type Failure = Status
The type to use when returning an Outcome::Failure
.
type Forward = Data
The type to use when returning an Outcome::Forward
.
Converts self
into an Outcome
. If self
represents a success, an Outcome::Success
is returned. Otherwise, an Outcome::Failure
is returned with failure
as the inner value. Read more
Converts self
into an Outcome
. If self
represents a success, an Outcome::Success
is returned. Otherwise, an Outcome::Forward
is returned with forward
as the inner value. Read more
The identity implementation of FromData
. Always returns Success
.
type Error = ()
The associated error to be returned when the guard fails.
Validates, parses, and converts an instance of Self
from the incoming request body data. Read more
Executes the destructor for this type. Read more
type Error = !
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more
Get the TypeId
of this object.