Rust Warp如何通过添加额外的标头将JSON有效载荷委托给另一个URL的请求?

发布于 2025-01-22 12:58:12 字数 845 浏览 0 评论 0原文

如何添加额外的标头,例如access_token:jshaj_some_really_long_token,还将content-type设置为content> content-type:application/json in Warp post post呼叫吗?

方案详细:

  1. 我想使用纱创建API,说终点是https://example.com/orders
  2. type是帖子,需要有效载荷=> '{“ Myid”:12,“ Price”:23.2,“ Transaction”:“ Buy”,“ dentity”:10}'
  3. 现在我想使用reqwest或构建warp http客户端的任何内容都将请求发送到另一个服务器端点SAIS SAIS SAI SAI SAS https://api.example.com/orders.com/orders带有上述后有效负载,但使用新标头content-type:application/jsonaccess-token:jshaj_some_really_long_token

如果有一些使用Warp and Reqwest的示例,请共享用于发布请求。目前,我正在引用 this

How to add extra header like access_token : jshaj_some_really_long_token and also set content-type to Content-Type: application/json in warp post call?

Scenario in detail:

  1. I want to create API using warp say the end point is https://example.com/orders
  2. The type is post which needs a payload => '{ "myid" : 12 , "price" : 23.2, "transaction" : "buy", "quantity" : 10 }'
  3. Now I want to use reqwest or any in built warp http client to send a request to another server endpoint say https://api.example.com/orders with the above post payload but with the new header which is Content-Type: application/json and access-token: jshaj_some_really_long_token

Please do share if there are some examples which are using warp and reqwest for post requests. Currently I am referring this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌枕肩 2025-01-29 12:58:12

您可以使用 warp-reverse-reverse-proxy那个(在这里维护器:))

您必须在内部组合实用程序,以根据示例修改您的需求:

#[tokio::main]
async fn main() {
    let hello = warp::path!("hello" / String).map(|name| format!("Hello port, {}!", name));

    // // spawn base server
    tokio::spawn(warp::serve(hello).run(([0, 0, 0, 0], 8080)));

    let request_filter = extract_request_data_filter();
    let app = warp::path!("hello" / String)
        // build proxy address and base path data from current filter
        .map(|port| (format!("http://127.0.0.1:{}/", port), "".to_string()))
        .untuple_one()
        // build the request with data from previous filters
        .and(request_filter)
        .and_then(proxy_to_and_forward_response)
        .and_then(log_response);

    // spawn proxy server
    warp::serve(app).run(([0, 0, 0, 0], 3030)).await;
}

请注意,请求的转发是由LIB完成的,如果您实际想要示例,则可以检查源代码调查如何完成。

You can use the warp-reverse-proxy crate for that (maintainer here :) )

You would have to compose the utilities inside to modify what you need as per the example:

#[tokio::main]
async fn main() {
    let hello = warp::path!("hello" / String).map(|name| format!("Hello port, {}!", name));

    // // spawn base server
    tokio::spawn(warp::serve(hello).run(([0, 0, 0, 0], 8080)));

    let request_filter = extract_request_data_filter();
    let app = warp::path!("hello" / String)
        // build proxy address and base path data from current filter
        .map(|port| (format!("http://127.0.0.1:{}/", port), "".to_string()))
        .untuple_one()
        // build the request with data from previous filters
        .and(request_filter)
        .and_then(proxy_to_and_forward_response)
        .and_then(log_response);

    // spawn proxy server
    warp::serve(app).run(([0, 0, 0, 0], 3030)).await;
}

Notice that the forwarding of the request is done by the lib, if you actually want examples you can check the source code to investigate how it is done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文