Rust Rocket,如何从请求的URI查询中获取数据

huangapple go评论71阅读模式
英文:

Rust Rocket, how to Access Data from a Request URI Query

问题

我正忙于使用Rust Rocket应用程序工作,我想知道是否可以从FromRequest实现内部访问请求URI查询对象的数据

我有一个示例的FromRequest实现

```rust
use rocket;
use rocket::{Config, Rocket, FromForm};
use rocket::request::{FromRequest, Request, Outcome};

#[tokio::main]
async fn main() {
    let figment = Config::figment().merge(("address", "0.0.0.0")).merge(("port", 31));
    let _rocket = Rocket::build()
        .configure(figment)
        .mount("/", rocket::routes![index])
        .launch().await.expect("ERROR");
}

#[rocket::get("/")]
fn index(_query_params: QueryParams) -> &'static str {
    return "Index";
}

#[derive(FromForm)]
pub struct QueryParams { _nothing: String }

#[rocket::async_trait]
impl<'r> FromRequest<'r> for QueryParams {
    type Error = rocket::Error;
    async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
        // 获取查询对象
        let query = request.uri().query().unwrap();
        // 检查查询的内容
        println!("{:?}", query);
        // 下面的代码不起作用,因为它们是私有字段
        let data_value = query.data.value;
        Outcome::Success(QueryParams { _nothing: String::from("nothing") })
    }
}

使用rocket = "0.5.0-rc.2"

如果我发送一个测试GET请求:

http://url.com:31/?param01%5Bkey01%5D=val01&param01%5Bkey02%5D=val02

我可以在调试输出中得到以下结果:

Query { source: None, data: Data { value: "param01%5Bkey01%5D=val01&param01%5Bkey02%5D=val02", decoded_segments: [uninitialized storage] } }

我可以看到值在那里,看起来是正确的,但是在这一点上我找不到任何访问它的方法,因为它们在私有字段后面。

英文:

I'm busy working with a Rust Rocket application and I was wondering if it is possible to access the data from a Request URI Query object from inside of a FromRequest implementation.

I have an example FromRequest implementation as

use rocket;
use rocket::{Config, Rocket, FromForm};
use rocket::request::{FromRequest, Request, Outcome};

#[tokio::main]
async fn main() {
    let figment = Config::figment().merge((&quot;address&quot;, &quot;0.0.0.0&quot;)).merge((&quot;port&quot;, 31));
    let _rocket = Rocket::build()
        .configure(figment)
        .mount(&quot;/&quot;, rocket::routes![index])
        .launch().await.expect(&quot;ERROR&quot;);
}

#[rocket::get(&quot;/&quot;)]
fn index(_query_params:QueryParams) -&gt; &amp;&#39;static str {
    return &quot;Index&quot;;
}

#[derive(FromForm)]
pub struct QueryParams { _nothing:String }

#[rocket::async_trait]
impl&lt;&#39;r&gt; FromRequest&lt;&#39;r&gt; for QueryParams {
    type Error = rocket::Error;
    async fn from_request(request: &amp;&#39;r Request&lt;&#39;_&gt;) -&gt; Outcome&lt;Self, Self::Error&gt; {
        // get the query object
        let query = request.uri().query().unwrap();
        // check the contents of the query
        println!(&quot;{:?}&quot;, query);
        // below does not work because they are private fields
        let data_value = query.data.value;
        Outcome::Success(QueryParams { _nothing:String::from(&quot;nothing&quot;) })
    }
}

Using rocket = &quot;0.5.0-rc.2&quot;

If I do a test get request as

http://url.com:31/?param01%5Bkey01%5D=val01&amp;param01%5Bkey02%5D=val02

I can get the following with the Debug Output

Query { source: None, data: Data { value: &quot;param01%5Bkey01%5D=val01&amp;param01%5Bkey02%5D=val02&quot;, decoded_segments: [uninitialized storage] } }

I can see the value is there and looks correct but I can't find any way of accessing it at this point because they are behind private fields.

答案1

得分: 1

For version 0.4: 使用Iterator实现来获取一系列FormItem,这些FormItem具有rawkeyvalue字段,类型为RawStr

For version 0.5-rc: 使用此处列出的Query上的方法:https://api.rocket.rs/v0.5-rc/rocket/http/uri/struct.Query.html#impl,或者,因为DerefRawStr,您可以使用RawStr的任何方法。

英文:

For version 0.4: Use the Iterator implementation to get a sequence of FormItems, which have public fields raw, key, and value of type RawStr.

For version 0.5-rc: Use the methods on Query listed here: https://api.rocket.rs/v0.5-rc/rocket/http/uri/struct.Query.html#impl, or, since it Derefs to RawStr, you can use any of RawStr's methods.

huangapple
  • 本文由 发表于 2023年3月4日 01:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定