标签: hyper

如何使用 Rust 中的客户端证书发出请求

我有一个在 Bluemix 中使用 Docker 容器部署微服务的项目。所有微服务都是用 Java 编写的,通信使用 JKS 文件。

我还在 Node.js 中使用 Express.js 开发了一个微服务。为了使用其他微服务,我使用带有option.agentOptionsfeature 和 a的 Request 模块pfx file,如下所示:

var options = {
        uri: config.get("https://www.example.com/ms/service"),
        method: 'POST',
        body: data,
        json: true,
        headers: {
            'Content-Type': 'application/json; charset=UTF-8'
        },
        agentOptions: {
            pfx: fs.readFileSync(config.get("/path/to/file.pfx")),
            passphrase: config.get("passphraseText"),
            servername: config.get("serverName")
        }
    };

request(options, function (error, response, data) {
     //handing response
});
Run Code Online (Sandbox Code Playgroud)

我尝试将 Solicit crate与HTTPS 的默认示例一起使用但它失败了:

var options = {
        uri: config.get("https://www.example.com/ms/service"),
        method: …
Run Code Online (Sandbox Code Playgroud)

ssl client-certificates ssl-certificate rust hyper

2
推荐指数
1
解决办法
2946
查看次数

创建具有自定义错误类型的超级服务

我正在尝试使用 hyper 创建 REST 服务器。对于稳健的错误处理,我希望服务返回一个带有自定义错误类型的未来,该类型包含 hyper、Diesel 和其他错误。不幸的是,hyper::Response似乎对错误类型的流进行了硬编码hyper::error::Error,这与我为我的服务定义的错误类型相冲突。我看到了几个可能的解决方案:

  1. 通过修改 使我的服务返回我的自定义错误类型hyper::Response,这似乎很难。

  2. 将非超级错误包装在hyper::error::Error. 这看起来很hacky。

  3. 别的东西。似乎我错过了执行此操作的“正确”方法。

以下代码显示了我认为我想做的事情:

extern crate diesel;
extern crate futures;
extern crate hyper;

use futures::future::{ok, Future};
use hyper::StatusCode;
use hyper::server::{Request, Response, Service};

fn main() {
    let address = "127.0.0.1:8080".parse().unwrap();
    let server = hyper::server::Http::new()
        .bind(&address, move || Ok(ApiService {}))
        .unwrap();
    server.run().unwrap();
}

pub struct ApiService;

impl Service for ApiService {
    type Request = Request;
    type Response = Response;
    type Error = Error;
    type Future …
Run Code Online (Sandbox Code Playgroud)

rust hyper

2
推荐指数
1
解决办法
1744
查看次数

如何与超级请求处理程序共享不可变的配置数据?

我正在尝试在 Rust 中开发一个基于超级服务器的应用程序。有一个 INI 文件保存配置,如绑定 IP、数据库等。

我不想在每个请求上解析 INI 文件,并且可以保留配置数据直到服务器重新启动。如何将已解析数据的结构提供给请求处理程序?

我尝试了多种方法,例如 using std::sync::Arc,但目前唯一有效的方法是使用 a static,但我想避免unsafe阻塞。

这是一个完整的(非工作)示例:

Cargo.toml

[package]
name = "demo"
version = "0.1.0"
edition = "2018"

[dependencies]
hyper = "0.12"
rust-ini = "0.13"
Run Code Online (Sandbox Code Playgroud)

演示文件

[Demo]
value="some value"
Run Code Online (Sandbox Code Playgroud)

src/main.rs

[package]
name = "demo"
version = "0.1.0"
edition = "2018"

[dependencies]
hyper = "0.12"
rust-ini = "0.13"
Run Code Online (Sandbox Code Playgroud)

错误

[Demo]
value="some value"
Run Code Online (Sandbox Code Playgroud)

rust hyper

2
推荐指数
1
解决办法
383
查看次数

期货-预期(),找到结构期货::地图

我试图用Hyper发送一个请求,然后通过Serde通过JSON反序列化它,但是我似乎无法束手无策,我收到了类型不匹配错误的说明expected (), found struct [put some odd struct here]。我也无法绕过每次更改都会吐出的令人难以置信的冗长而令人困惑的错误消息。这是我的代码:

extern crate futures;
extern crate hyper;
extern crate serde;
extern crate serde_json;

use futures::{
    Future,
    Stream,
    future
};
use hyper::{
    Body,
    Client,
    Response,
    StatusCode,
    Uri,
    client::HttpConnector,
};
use serde::{ Deserialize };
use std::error::{ Error };

enum JsonError
{
    RequestError(hyper::Error),
    ResponseError(StatusCode),
    DeserializeError(serde_json::Error),
}

fn get_json
    <'t, T, F>
    (client: &Client<HttpConnector>, uri: Uri)
-> impl Future<Item = T, Error = JsonError>
where
    T : Deserialize<'t> …
Run Code Online (Sandbox Code Playgroud)

rust hyper serde

2
推荐指数
1
解决办法
100
查看次数

Hyper POST请求总是产生400

我正在尝试使用Hyper 0.9向站点发送POST请求.该请求适用于curl:

curl https://api.particle.io/v1/devices/secret/set_light -d args=0 -d access_token=secret
Run Code Online (Sandbox Code Playgroud)

和Python:

import requests
r = requests.post("https://api.particle.io/v1/devices/secret/set_light",
    data={"access_token": "secret", "args": "0"})
Run Code Online (Sandbox Code Playgroud)

但我的Rust实现似乎没有通过,总是屈服400.

use hyper::client::Client;

let addr = "https://api.particle.io/v1/devices/secret/set_light";
let body = "access_token=secret&args=0";
let mut res = client.post(addr)
                .body(body)
                .send()
                .unwrap();
Run Code Online (Sandbox Code Playgroud)

post rust hyper

1
推荐指数
1
解决办法
1026
查看次数

无法读取通过hyper :: client :: Client发出HTTP请求的简单有效负载:特征绑定“ Body:Future”不满足

我正在尝试将转换ResultBuffer

let ufc_root: String =
    String::from("https://www.ufc.com/athletes/all?filters%5B0%5D=status%3A23");
// let ufc_root: String = String::from("https://www.google.com");
let https = HttpsConnector::new(4).unwrap();
let client = Client::builder().build::<_, hyper::Body>(https);

client
    .get(ufc_root.parse::<hyper::Uri>().unwrap())
    .and_then(|res| {
        println!("http status code: {}", res.status());
        println!("http response headers:\n{:?}: ", res.headers());
        res.into_body()
    })
    .from_err::<WebScrapeError>()
    .and_then(|body| {
        body.for_each(|chunk| {
            println!("{}", chunk.into_bytes());
        });

        let jon_jones = Subject {
            name: "Jon Jones".to_string(),
            link: "http://www.jonjones.com".to_string(),
        };
        let subjects = vec![jon_jones];
        Ok(subjects)
    })
    .from_err()
Run Code Online (Sandbox Code Playgroud)
let ufc_root: String =
    String::from("https://www.ufc.com/athletes/all?filters%5B0%5D=status%3A23");
// let ufc_root: String = String::from("https://www.google.com");
let https …
Run Code Online (Sandbox Code Playgroud)

future rust hyper

1
推荐指数
1
解决办法
208
查看次数

导入时生锈"预期标识符,找到关键字"

我有两个文件,loop.rs包含一个函数请求来实例化客户端并获取一个网页的主体.我想将请求导出到main.我知道要出口我需要mod file_to_import然后use file_to_import::function_to_use根据这篇文章

src/
   main.rs
   loop.rs


// loop.rs ->
//crates
extern crate futures;
extern crate hyper;
extern crate tokio_core;
use std::io::{self, Write};
use self::futures::{Future, Stream};
use self::hyper::Client;
use self::tokio_core::reactor::Core;


//request function to be exported to main.rs

pub fn request(url: &str)  {
   let mut core = Core::new().unwrap();
   let client = Client::new(&core.handle());
   let uri = url.parse().unwrap();
   let work = client.get(uri).and_then(|res| {
      println!("Response: {}", res.status());

      res.body().for_each(|chunk| {
         io::stdout()
         .write_all(&chunk)
         .map_err(From::from)
     })
});
core.run(work).unwrap();
}


// main.rs ->
mod …
Run Code Online (Sandbox Code Playgroud)

rust hyper

0
推荐指数
1
解决办法
1369
查看次数

在Hyper 0.11中没有为类型`hyper :: Client`找到名为`post`的方法

我想用Hyper来制作HTTP请求.呼唤Client::get精细的作品,但其他方法如Client::postClient::head引起编译错误.

extern crate futures;
extern crate hyper;
extern crate tokio_core;

use std::io::{self, Write};
use futures::{Future, Stream};
use hyper::Client;
use tokio_core::reactor::Core;

fn main() {
    let mut core = Core::new().unwrap();
    let client = Client::new(&core.handle());

    let uri = "http://httpbin.org/ip".parse().unwrap();
    let work = client.post(uri).and_then(|res| {
        // if post changed to get it will work correctly
        println!("Response: {}", res.status());

        res.body("x=z")
            .for_each(|chunk| io::stdout().write_all(&chunk).map_err(From::from))
    });
    core.run(work).unwrap();
}
Run Code Online (Sandbox Code Playgroud)

错误:

error[E0599]: no method named `post` found for type `hyper::Client<hyper::client::HttpConnector>` in the current scope …
Run Code Online (Sandbox Code Playgroud)

rust hyper

0
推荐指数
1
解决办法
624
查看次数

标签 统计

hyper ×8

rust ×8

client-certificates ×1

future ×1

post ×1

serde ×1

ssl ×1

ssl-certificate ×1