首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Hyper 0.11中没有为“Hyper::Client`”类型找到名为‘post`的方法

在Hyper 0.11中没有为“Hyper::Client`”类型找到名为‘post`的方法
EN

Stack Overflow用户
提问于 2018-02-01 22:53:49
回答 1查看 1.2K关注 0票数 1

我想使用Hyper来制作HTTP请求。调用Client::get可以正常工作,但其他方法(如Client::postClient::head )会导致编译错误。

代码语言:javascript
复制
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();
}

错误:

代码语言:javascript
复制
error[E0599]: no method named `post` found for type `hyper::Client<hyper::client::HttpConnector>` in the current scope
  --> src/main.rs:15:23
   |
15 |     let work = client.post(uri).and_then(|res| {
   |                       ^^^^

error[E0277]: the trait bound `[u8]: std::marker::Sized` is not satisfied
  --> src/main.rs:20:24
   |
20 |             .for_each(|chunk| io::stdout().write_all(&chunk).map_err(From::from))
   |                        ^^^^^ `[u8]` does not have a constant size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `[u8]`
   = note: all local variables must have a statically known size
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-01 23:17:06

错误信息没有秘密的诡计。您将得到错误“named post for type hyper::Client”,因为没有这样的方法。

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

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

代码语言:javascript
复制
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| {
        // ...
    });
}

纸箱文件说:

如果刚开始,会查看 指南 first

对于您的案例,有一个指南:高级客户端使用

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48573070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档