pub struct Request<State> { /* private fields */ }
Expand description

HTTP request.

请求、路由参数以及访问请求的各种方式。 中间件和endpoints之间的通信

Implementations

访问请求的HTTP方法。

Examples
use summer_boot::Request;

let mut app = summer_boot::new();
app.at("/").get(|req: Request<()>| async move {
    assert_eq!(req.method(), http_types::Method::Get);
    Ok("")
});
app.listen("127.0.0.1:8080").await?;

访问请求的完整URI方法。

访问请求的HTTP版本。

Examples
use summer_boot::Request;

let mut app = summer_boot::new();
app.at("/").get(|req: Request<()>| async move {
    assert_eq!(req.version(), Some(http_types::Version::Http1_1));
    Ok("")
});
app.listen("127.0.0.1:8080").await?;

获取基础传输的socket地址

获取基础传输的本地地址

获取此请求的远程地址。

按以下优先级确定:

  1. Forwarded head for key
  2. 第一个 X-Forwarded-For header
  3. 传输的对等地址

获取此请求的目标主机。

按以下优先级确定:

  1. Forwarded header host key
  2. 第一个 X-Forwarded-Host header
  3. Host header
  4. URL域

以“Mime”形式获取请求内容类型。

这将获取请求 Content-Type header。

获取HTTP header.

Examples
use summer_boot::Request;

let mut app = summer_boot::new();
app.at("/").get(|req: Request<()>| async move {
    assert_eq!(req.header("X-Forwarded-For").unwrap(), "127.0.0.1");
    Ok("")
});
app.listen("127.0.0.1:8080").await?;

获取标题的可变引用。

设置一个 HTTP header.

将header添加到headers。

insert 不同,此函数不会重写标头的内容,而是插入 如果没有header。添加到现有的headers列表中。

移除一个 header.

以任意顺序访问所有header的迭代。

迭代器以任意顺序访问所有header,并对值进行可变引用。

以任意顺序访问所有header名称的迭代。

以任意顺序访问所有header值的迭代。

获取请求扩展值。

获取对存储在请求扩展中的值的可变引用。

设置请求扩展值。

访问应用程序范围的状态。

按名称提取和解析路由参数。

&str 形式返回参数,该参数是从此 Request 借用的。

名称应不包括引用 :

Errors

如果 key 不是路由的有效参数,则返回错误。

Examples
use summer_boot::{Request, Result};

async fn greet(req: Request<()>) -> Result<String> {
    let name = req.param("name").unwrap_or("world");
    Ok(format!("Hello, {}!", name))
}

let mut app = summer_boot::new();
app.at("/hello").get(greet);
app.at("/hello/:name").get(greet);
app.listen("127.0.0.1:8080").await?;

从路由中提取通配符(如果存在)

&str 形式返回参数,该参数是从此 Request 借用的。

Examples
use summer_boot::{Request, Result};

async fn greet(req: Request<()>) -> Result<String> {
    let name = req.wildcard().unwrap_or("world");
    Ok(format!("Hello, {}!", name))
}

let mut app = summer_boot::new();
app.at("/hello/*").get(greet);
app.listen("127.0.0.1:8080").await?;

使用serde_qs将URL查询组件解析为结构 将整个查询作为未解析的字符串获取,使用 request.url().query()

Examples
use std::collections::HashMap;
use summer_boot::http_types::{self, convert::Deserialize};
use summer_boot::Request;

// 所有权结构:

#[derive(Deserialize)]
struct Index {
    page: u32,
    selections: HashMap<String, String>,
}

let req: Request<()> = http_types::Request::get("https://baidu.com/get?page=2&selections[width]=narrow&selections[height]=tall").into();
let Index { page, selections } = req.query().unwrap();
assert_eq!(page, 2);
assert_eq!(selections["width"], "narrow");
assert_eq!(selections["height"], "tall");

// 使用借用s:

#[derive(Deserialize)]
struct Query<'q> {
    format: &'q str,
}

let req: Request<()> = http_types::Request::get("https://httpbin.org/get?format=bananna").into();
let Query { format } = req.query().unwrap();
assert_eq!(format, "bananna");

设置body读取

处理请求 Body

可以在获取或读取body后调用此方法, 但是将返回一个空的Body.

这对于通过AsyncReader或AsyncBufReader有用。

将整个请求body读取字节缓冲区。

可以在读取body后调用此方法,但生成空缓冲区。

Errors

读取body时遇到的任何I/O错误都会立即返回错误 Err

Examples
use summer_boot::Request;

let mut app = summer_boot::new();
app.at("/").get(|mut req: Request<()>| async move {
    let _body: Vec<u8> = req.body_bytes().await.unwrap();
    Ok("")
});
app.listen("127.0.0.1:8080").await?;

将整个请求body读取字符串。

可以在读取body后调用此方法,但生成空缓冲区。

Errors

读取body时遇到的任何I/O错误都会立即返回错误 Err

如果body不能解释有效的UTF-8,则返回 Err

Examples
use summer_boot::Request;

let mut app = summer_boot::new();
app.at("/").get(|mut req: Request<()>| async move {
    let _body: String = req.body_string().await.unwrap();
    Ok("")
});
app.listen("127.0.0.1:8080").await?;

通过json读取并反序列化整个请求body。

Errors

读取body时遇到的任何I/O错误都会立即返回错误 Err

如果无法将body解释为目标类型 T 的有效json,则返回 Err

将请求主体解析为表单

use serde::Deserialize;
let mut app = summer_boot::new();

#[derive(Deserialize)]
struct Animal {
  name: String,
  legs: u8
}

app.at("/").post(|mut req: summer_boot::Request<()>| async move {
    let animal: Animal = req.body_form().await?;
    Ok(format!(
        "hello, {}! i've put in an order for {} shoes",
        animal.name, animal.legs
    ))
});

app.listen("localhost:8000").await?;

// $ curl localhost:8000/test/api -d "name=chashu&legs=4"
// hello, chashu! i've put in an order for 4 shoes

// $ curl localhost:8000/test/api -d "name=mary%20millipede&legs=750"
// number too large to fit in target type

获取body流的长度(如果已设置)。

将固定大小的对象传递到作为body时,会设置此值(比如字符串)。 或者缓冲区。 此API的使用应检查此值,决定是否使用 Chunked 设置响应长度

如果请求的设置body流长度为零,则返回 true,否则返回 false

Trait Implementations

Converts this type into a mutable reference of the (usually inferred) input type.

Converts this type into a mutable reference of the (usually inferred) input type.

Converts this type into a shared reference of the (usually inferred) input type.

Converts this type into a shared reference of the (usually inferred) input type.

Attempt to read from the AsyncRead into buf. Read more

Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

返回对与提供的名称相对应的值的引用。

Panics

如果 Request 中没有该名称,则会panic

The returned type after indexing.

返回对与提供的名称相对应的值的引用。

Panics

如果 Request 中没有该名称,则会panic

The returned type after indexing.

返回对其余项的引用的迭代.

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Reads some bytes from the byte stream. Read more

Like [read()][AsyncReadExt::read()], except it reads into a slice of buffers. Read more

Reads the entire contents and appends them to a Vec. Read more

Reads the entire contents and appends them to a String. Read more

Reads the exact number of bytes required to fill buf. Read more

Creates an adapter which will read at most limit bytes from it. Read more

Converts this [AsyncRead] into a [Stream] of bytes. Read more

Creates an adapter which will chain this stream with another. Read more

Boxes the reader and changes its type to dyn AsyncRead + Send + 'a. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Reads some bytes from the byte stream. Read more

Like read, except that it reads into a slice of buffers. Read more

Reads all bytes from the byte stream. Read more

Reads all bytes from the byte stream and appends them into a string. Read more

Reads the exact number of bytes required to fill buf. Read more

Creates an adaptor which will read at most limit bytes from it. Read more

Creates a “by reference” adaptor for this instance of Read. Read more

Transforms this Read instance to a Stream over its bytes. Read more

Creates an adaptor which will chain this stream with another. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.