英文:
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¶m01%5Bkey02%5D=val02
我可以在调试输出中得到以下结果:
Query { source: None, data: Data { value: "param01%5Bkey01%5D=val01¶m01%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(("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> {
// get the query object
let query = request.uri().query().unwrap();
// check the contents of the query
println!("{:?}", query);
// below does not work because they are private fields
let data_value = query.data.value;
Outcome::Success(QueryParams { _nothing:String::from("nothing") })
}
}
Using rocket = "0.5.0-rc.2"
If I do a test get request as
http://url.com:31/?param01%5Bkey01%5D=val01&param01%5Bkey02%5D=val02
I can get the following with the Debug Output
Query { source: None, data: Data { value: "param01%5Bkey01%5D=val01&param01%5Bkey02%5D=val02", 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
具有raw
,key
和value
字段,类型为RawStr
。
For version 0.5-rc: 使用此处列出的Query上的方法:https://api.rocket.rs/v0.5-rc/rocket/http/uri/struct.Query.html#impl,或者,因为它Deref
到RawStr
,您可以使用RawStr
的任何方法。
英文:
For version 0.4: Use the Iterator
implementation to get a sequence of FormItem
s, 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 Deref
s to RawStr
, you can use any of RawStr
's methods.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论