Struct rocket::local::blocking::LocalResponse
source · pub struct LocalResponse<'c> { /* private fields */ }
Expand description
A blocking
response from a dispatched LocalRequest
.
This LocalResponse
implements io::Read
. As such, if
into_string()
and
into_bytes()
do not suffice, the response’s
body can be read directly:
use std::io::{self, Read};
use rocket::local::blocking::Client;
use rocket::http::Status;
#[get("/")]
fn hello_world() -> &'static str {
"Hello, world!"
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![hello_world])
}
// Dispatch a `GET /` request.
let client = Client::tracked(rocket()).expect("valid rocket");
let mut response = client.get("/").dispatch();
// Check metadata validity.
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.body().preset_size(), Some(13));
// Read 10 bytes of the body. Note: in reality, we'd use `into_string()`.
let mut buffer = [0; 10];
response.read(&mut buffer)?;
assert_eq!(buffer, "Hello, wor".as_bytes());
For more, see the top-level documentation.
Implementations§
source§impl LocalResponse<'_>
impl LocalResponse<'_>
sourcepub fn status(&self) -> Status
pub fn status(&self) -> Status
Returns the
HTTP status
of self
.
Example
use rocket::local::blocking::LocalResponse;
let response: LocalResponse = response;
let status = response.status();
sourcepub fn content_type(&self) -> Option<ContentType>
pub fn content_type(&self) -> Option<ContentType>
Returns the
Content-Type, if a valid one is set,
of self
.
Example
use rocket::local::blocking::LocalResponse;
let response: LocalResponse = response;
let content_type = response.content_type();
sourcepub fn headers(&self) -> &HeaderMap<'_>
pub fn headers(&self) -> &HeaderMap<'_>
Returns the
HTTP headers
of self
.
Example
use rocket::local::blocking::LocalResponse;
let response: LocalResponse = response;
let headers = response.headers();
Return a cookie jar containing the HTTP cookies in the response.
Example
use rocket::local::blocking::LocalResponse;
let response: LocalResponse = response;
let string = response.cookies();
sourcepub fn body(&self) -> &Body<'_>
pub fn body(&self) -> &Body<'_>
Returns the
response body, if there is one,
of self
.
Example
use rocket::local::blocking::LocalResponse;
let response: LocalResponse = response;
let body = response.body();
sourcepub fn into_string(self) -> Option<String>
pub fn into_string(self) -> Option<String>
Consumes self
and reads the entirety of its body into a string.
If reading fails, the body contains invalid UTF-8 characters, or the
body is unset in the response, returns None
. Otherwise, returns
Some
. The string may be empty if the body is empty.
Example
use rocket::local::blocking::LocalResponse;
let response: LocalResponse = response;
let string = response.into_string();
sourcepub fn into_bytes(self) -> Option<Vec<u8>>
pub fn into_bytes(self) -> Option<Vec<u8>>
Consumes self
and reads the entirety of its body into a Vec
of
bytes.
If reading fails or the body is unset in the response, returns None
.
Otherwise, returns Some
. The returned vector may be empty if the body
is empty.
Example
use rocket::local::blocking::LocalResponse;
let response: LocalResponse = response;
let bytes = response.into_bytes();
sourcepub fn into_json<T>(self) -> Option<T>where
T: Send + DeserializeOwned + 'static,
Available on crate feature json
only.
pub fn into_json<T>(self) -> Option<T>where T: Send + DeserializeOwned + 'static,
json
only.Consumes self
and deserializes its body as JSON without buffering in
memory.
If deserialization fails or the body is unset in the response, returns
None
. Otherwise, returns Some
.
Example
use rocket::local::blocking::LocalResponse;
use rocket::serde::Deserialize;
#[derive(Deserialize)]
struct Task {
id: usize,
complete: bool,
text: String,
}
let response: LocalResponse = response;
let task = response.into_json::<Task>();
sourcepub fn into_msgpack<T>(self) -> Option<T>where
T: Send + DeserializeOwned + 'static,
Available on crate feature msgpack
only.
pub fn into_msgpack<T>(self) -> Option<T>where T: Send + DeserializeOwned + 'static,
msgpack
only.Consumes self
and deserializes its body as MessagePack without
buffering in memory.
If deserialization fails or the body is unset in the response, returns
None
. Otherwise, returns Some
.
Example
use rocket::local::blocking::LocalResponse;
use rocket::serde::Deserialize;
#[derive(Deserialize)]
struct Task {
id: usize,
complete: bool,
text: String,
}
let response: LocalResponse = response;
let task = response.into_msgpack::<Task>();
Trait Implementations§
source§impl Debug for LocalResponse<'_>
impl Debug for LocalResponse<'_>
source§impl Read for LocalResponse<'_>
impl Read for LocalResponse<'_>
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
buf
. Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,
Read
. Read moreAuto Trait Implementations§
impl<'c> !RefUnwindSafe for LocalResponse<'c>
impl<'c> !Send for LocalResponse<'c>
impl<'c> !Sync for LocalResponse<'c>
impl<'c> Unpin for LocalResponse<'c>
impl<'c> !UnwindSafe for LocalResponse<'c>
Blanket Implementations§
§impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for Twhere T: 'a,
§impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for Twhere T: 'a,
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
§fn into_collection<A>(self) -> SmallVec<A>where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A>where A: Array<Item = T>,
self
into a collection.fn mapped<U, F, A>(self, f: F) -> SmallVec<A>where F: FnMut(T) -> U, A: Array<Item = U>,
§impl<R> ReadBytesExt for Rwhere
R: Read + ?Sized,
impl<R> ReadBytesExt for Rwhere R: Read + ?Sized,
§fn read_u8(&mut self) -> Result<u8, Error>
fn read_u8(&mut self) -> Result<u8, Error>
§fn read_i8(&mut self) -> Result<i8, Error>
fn read_i8(&mut self) -> Result<i8, Error>
§fn read_u16<T>(&mut self) -> Result<u16, Error>where
T: ByteOrder,
fn read_u16<T>(&mut self) -> Result<u16, Error>where T: ByteOrder,
§fn read_i16<T>(&mut self) -> Result<i16, Error>where
T: ByteOrder,
fn read_i16<T>(&mut self) -> Result<i16, Error>where T: ByteOrder,
§fn read_u24<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
fn read_u24<T>(&mut self) -> Result<u32, Error>where T: ByteOrder,
§fn read_i24<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
fn read_i24<T>(&mut self) -> Result<i32, Error>where T: ByteOrder,
§fn read_u32<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
fn read_u32<T>(&mut self) -> Result<u32, Error>where T: ByteOrder,
§fn read_i32<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
fn read_i32<T>(&mut self) -> Result<i32, Error>where T: ByteOrder,
§fn read_u48<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
fn read_u48<T>(&mut self) -> Result<u64, Error>where T: ByteOrder,
§fn read_i48<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
fn read_i48<T>(&mut self) -> Result<i64, Error>where T: ByteOrder,
§fn read_u64<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
fn read_u64<T>(&mut self) -> Result<u64, Error>where T: ByteOrder,
§fn read_i64<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
fn read_i64<T>(&mut self) -> Result<i64, Error>where T: ByteOrder,
§fn read_u128<T>(&mut self) -> Result<u128, Error>where
T: ByteOrder,
fn read_u128<T>(&mut self) -> Result<u128, Error>where T: ByteOrder,
§fn read_i128<T>(&mut self) -> Result<i128, Error>where
T: ByteOrder,
fn read_i128<T>(&mut self) -> Result<i128, Error>where T: ByteOrder,
§fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where
T: ByteOrder,
fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where T: ByteOrder,
§fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where
T: ByteOrder,
fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where T: ByteOrder,
§fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where
T: ByteOrder,
fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where T: ByteOrder,
§fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where
T: ByteOrder,
fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where T: ByteOrder,
§fn read_f32<T>(&mut self) -> Result<f32, Error>where
T: ByteOrder,
fn read_f32<T>(&mut self) -> Result<f32, Error>where T: ByteOrder,
§fn read_f64<T>(&mut self) -> Result<f64, Error>where
T: ByteOrder,
fn read_f64<T>(&mut self) -> Result<f64, Error>where T: ByteOrder,
§fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where
T: ByteOrder,
fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where T: ByteOrder,
§fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where
T: ByteOrder,
fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where T: ByteOrder,
§fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where
T: ByteOrder,
fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where T: ByteOrder,
§fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where
T: ByteOrder,
fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where T: ByteOrder,
§fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
§fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where
T: ByteOrder,
fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where T: ByteOrder,
§fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where
T: ByteOrder,
fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where T: ByteOrder,
§fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where
T: ByteOrder,
fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where T: ByteOrder,
§fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where
T: ByteOrder,
fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where T: ByteOrder,
§fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where T: ByteOrder,
§fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where T: ByteOrder,
read_f32_into
instead