Rust 在hyper 0.11中找不到类型为“hyper::Client”的名为“post”的方法

Rust 在hyper 0.11中找不到类型为“hyper::Client”的名为“post”的方法,rust,hyper,Rust,Hyper,我想使用Hyper来制作HTTP请求。调用Client::get可以正常工作,但其他方法,如Client::post和Client::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::

我想使用Hyper来制作HTTP请求。调用
Client::get
可以正常工作,但其他方法,如
Client::post
Client::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();
}
错误:

error[E0599]:在当前作用域中找不到类型为“hyper::Client”的名为“post”的方法
-->src/main.rs:15:23
|
15 | let work=client.post(uri)。然后| res |{
|                       ^^^^
错误[E0277]:未满足特征绑定“[u8]:std::marker::Sized”
-->src/main.rs:20:24
|
20 |.for| u each(| chunk | io::stdout().write|u all(&chunk).map|u err(From::From))
|^^`[u8]`在编译时没有已知的常量大小
|
=help:trait`std::marker::Sized`未为`[u8]实现`
=注意:所有局部变量必须具有静态已知的大小

错误消息中没有秘密欺骗。您将收到错误“没有为类型
hyper::Client
找到名为
post
的方法”,因为没有此类方法

如果您查看的文档,您可以看到它的所有方法。它们都不是
post

相反,您需要使用
Client::request
并传入一个值。
request
的构造函数接受一个表示要使用的HTTP方法的值

use hyper::{Client, Request, Method};

fn main() {
    // ...

    let uri = "http://httpbin.org/ip".parse().unwrap();
    let req = Request::new(Method::Post, uri);

    let work = client.request(req).and_then(|res| {
        // ...
    });
}
报告说:

如果刚开始,请查看第一个


对于您的案例,有一个指南:。

错误消息中没有秘密欺骗。您将收到错误“没有为类型
hyper::Client
找到名为
post
的方法”,因为没有此类方法

如果您查看的文档,您可以看到它的所有方法。它们都不是
post

相反,您需要使用
Client::request
并传入一个值。
request
的构造函数接受一个表示要使用的HTTP方法的值

use hyper::{Client, Request, Method};

fn main() {
    // ...

    let uri = "http://httpbin.org/ip".parse().unwrap();
    let req = Request::new(Method::Post, uri);

    let work = client.request(req).and_then(|res| {
        // ...
    });
}
报告说:

如果刚开始,请查看第一个


有一个指南正好适合您的情况:。

hyper::Client
没有post方法!请参阅:。它只有
get()
request()
Client.request(request::new(method::post,uri))
是合适的。@daboross看起来这是一个答案。你应该把它作为答案而不是评论发布。@Boiethios我会的,但现在它与Shepmaster的答案在很大程度上是多余的。
hyper::Client
没有post方法!请参见:。它只有
get()
request()
Client.request(request::new(Method::Post,uri))
是合适的。@daboross看起来这是一个答案。你应该将其作为答案而不是注释发布。@Boiethios我会的,但现在它与Shepmaster的答案在很大程度上是多余的。我无法使用《高级客户端使用指南》实现Post。毫无疑问,它指出:“记住,在我们把未来交给核心之前,岗位上的工作实际上什么都做不了。"@SteveB请确保您也阅读了前面的指南;据推测,《高级指南》是建立在之前更简单的指南基础上的。另外请注意,本问答是关于Hyper 0.11的,而当前版本的Hyper是0.12,为了适应未来,它做了很大的更改。我无法使用《高级客户端使用指南》实施POST。毫无疑问其中写道:“记住,在我们把未来交给核心之前,《邮报》的工作实际上什么都做不了。”@SteveB请确保您也阅读了前面的指南;据推测,《高级指南》是建立在之前更简单的指南基础上的。另外请注意,本问答是关于Hyper 0.11的,而当前版本的Hyper是0.12,为了适应未来,对其进行了重大更改。