pub struct Request {
pub body: Value,
pub headers: HashMap<String, String>,
pub query: HashMap<String, String>,
pub path_params: HashMap<String, String>,
/* private fields */
}Expand description
Action 请求
封装 HTTP 请求信息,用于 Action 执行
§字段
body: 请求体(JSON 格式)headers: 请求头query: 查询参数path_params: 路径参数
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;
use std::collections::HashMap;
// 创建请求
let mut request = Request::new(json!({
"username": "alice",
"email": "alice@example.com"
}));
// 添加请求头
request = request.header("Content-Type", "application/json");
request = request.header("Authorization", "Bearer token123");
// 添加查询参数
request = request.query("page", "1");
request = request.query("limit", "10");
// 添加路径参数
request = request.path_param("id", "123");
// 提取 Token
if let Some(token) = request.token() {
println!("Token: {}", token);
}Fields§
§body: Value请求体(JSON)
headers: HashMap<String, String>请求头
query: HashMap<String, String>查询参数
path_params: HashMap<String, String>路径参数
Implementations§
Source§impl Request
impl Request
Sourcepub fn headers(self, headers: HashMap<String, String>) -> Self
pub fn headers(self, headers: HashMap<String, String>) -> Self
批量添加请求头
§参数
headers: 请求头映射
§返回
- 修改后的 Request 实例(支持链式调用)
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;
use std::collections::HashMap;
let mut headers = HashMap::new();
headers.insert("Content-Type".to_string(), "application/json".to_string());
headers.insert("Authorization".to_string(), "Bearer token123".to_string());
let request = Request::new(json!({}))
.headers(headers);Sourcepub fn queries(self, query: HashMap<String, String>) -> Self
pub fn queries(self, query: HashMap<String, String>) -> Self
批量添加查询参数
§参数
query: 查询参数映射
§返回
- 修改后的 Request 实例(支持链式调用)
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;
use std::collections::HashMap;
let mut query = HashMap::new();
query.insert("page".to_string(), "1".to_string());
query.insert("limit".to_string(), "10".to_string());
let request = Request::new(json!({}))
.queries(query);Sourcepub fn path_params(self, path_params: HashMap<String, String>) -> Self
pub fn path_params(self, path_params: HashMap<String, String>) -> Self
批量添加路径参数
§参数
path_params: 路径参数映射
§返回
- 修改后的 Request 实例(支持链式调用)
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;
use std::collections::HashMap;
let mut path_params = HashMap::new();
path_params.insert("id".to_string(), "123".to_string());
path_params.insert("action".to_string(), "update".to_string());
let request = Request::new(json!({}))
.path_params(path_params);Sourcepub fn token(&self) -> Option<&str>
pub fn token(&self) -> Option<&str>
从 Authorization 头提取 Token
支持 Bearer Token 格式:Authorization: Bearer <token>
§返回
Some(&str): Token 字符串None: 未找到 Token 或格式不正确
§示例
ⓘ
use yang_base::action::Request;
use serde_json::json;
let request = Request::new(json!({}))
.header("Authorization", "Bearer my_secret_token");
assert_eq!(request.token(), Some("my_secret_token"));Sourcepub fn user_agent(&self) -> Option<&str>
pub fn user_agent(&self) -> Option<&str>
获取规范化 headers 中的 User-Agent。
获取规范化 headers 中的 Cookie。
Sourcepub fn get_header(&self, name: &str) -> Option<&str>
pub fn get_header(&self, name: &str) -> Option<&str>
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Request
impl !UnwindSafe for Request
impl Freeze for Request
impl Send for Request
impl Sync for Request
impl Unpin for Request
impl UnsafeUnpin for Request
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more