[][src]Struct hreq::server::Reply

pub struct Reply(_);

Concrete return type from endpoints and middleware.

This type will rarely be used directly. The return signature of endpoints and middleware is impl Into<Reply>. As a user of this crate you would prefer using one of the return types possible via the Into trait.

Return typeResponseNotes
()text/plain""
&strtext/plain"Hello"
Stringtext/plain"Hello".to_string()
&Stringtext/plain&"Hello".to_string()
&[u8]application/octet-streamb"abcdef"
Vec<u8>application/octet-streamvec![65,66,67]
&Vec<u8>application/octet-stream&vec![65,66,67]
std::fs::Fileapplication/octet-streamNB. Only with tokio
BodyBody determined
Result<Into<Body>, Into<Error>>See Body
Response<Into<Body>>Response and fallback to Body
Result<Response<Into<Body>>, Into<Error>>Response and falllback to Body
Option<Into<Reply>>404 on None

Examples

async fn handle(req: http::Request<hreq::Body>) {
    // 200 with empty reply
}
async fn handle(req: http::Request<hreq::Body>) -> &'static str {
    "Hello World!" // 200 with text/plain body
}
async fn handle(req: http::Request<hreq::Body>) -> &'static str {
    "Hello World!" // 200 with text/plain body
}
async fn handle(req: http::Request<hreq::Body>) -> String {
    format!("Hello there {}", req.uri().path()) // 200 with text/plain body
}
async fn handle(req: http::Request<hreq::Body>) -> &'static [u8] {
    &[79, 75, 10] // 200 with application/octet-stream
}
async fn handle(req: http::Request<hreq::Body>) -> Vec<u8> {
    vec![79, 75, 10] // 200 with application/octet-stream
}
use hreq::Body;

async fn handle(req: http::Request<Body>) -> Body {
    let data = vec![79, 75, 10];
    let len = data.len() as u64;
    let cursor = std::io::Cursor::new(data);
    Body::from_sync_read(cursor, Some(len)) // check Body doc for more
}
use hreq::Body;

async fn handle(
    req: http::Request<Body>
) -> Result<&'static str, hreq::Error> {
    let file = std::fs::File::open("/etc/bashrc")?;
    Ok("File opened OK")
}
use hreq::prelude::*;

async fn handle(req: http::Request<hreq::Body>) -> http::Response<()> {
    http::Response::builder()
        .status(302)
        .header("location", "/see-my-other-page")
        .body(())
        .unwrap()
}
use hreq::prelude::*;

async fn handle(
    req: http::Request<hreq::Body>
) -> http::Response<&'static str> {
    http::Response::builder()
        .header("X-My-Exotic-Header", "Cool")
        .body("Hello World!")
        .unwrap()
}
use hreq::prelude::*;

async fn handle(
    req: http::Request<hreq::Body>
) -> Result<http::Response<String>, hreq::Error> {
    let no = 42;

    Ok(http::Response::builder()
        .header("X-My-Exotic-Header", "Cool")
        .body(format!("Hello World!, {}", no))?)
}
use hreq::prelude::*;

async fn handle(
    req: http::Request<hreq::Body>
) -> Option<String> {
    let no = 42;

    Some(format!("Hello World!, {}", no))
}

Implementations

impl Reply[src]

pub fn into_result(self) -> Result<Response<Body>, Error>[src]

Unwrap the inner results.

Trait Implementations

impl Debug for Reply[src]

impl<'a> From<&'a [u8]> for Reply[src]

impl<'a> From<&'a String> for Reply[src]

impl<'a> From<&'a Vec<u8>> for Reply[src]

impl<'a> From<&'a str> for Reply[src]

impl<'a> From<()> for Reply[src]

impl From<Body> for Reply[src]

impl<R> From<Option<R>> for Reply where
    R: Into<Reply>, 
[src]

impl<B> From<Response<B>> for Reply where
    B: Into<Body>, 
[src]

impl<B, E> From<Result<B, E>> for Reply where
    B: Into<Body>,
    E: Into<Error>, 
[src]

impl<B, E> From<Result<Response<B>, E>> for Reply where
    B: Into<Body>,
    E: Into<Error>, 
[src]

impl From<String> for Reply[src]

impl From<Vec<u8>> for Reply[src]

Auto Trait Implementations

impl !RefUnwindSafe for Reply

impl Send for Reply

impl Sync for Reply

impl Unpin for Reply

impl !UnwindSafe for Reply

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Sealed<T> for T where
    T: ?Sized

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> WithSubscriber for T[src]