在 Hyper 0.11 中找不到名为 `post` 的方法用于 `hyper::Client` 类型
No method named `post` found for type `hyper::Client` in Hyper 0.11
我想使用 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]: 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
错误消息没有任何秘密诡计。您收到错误 "no method named post
found for type hyper::Client
" 因为没有这样的方法.
如果您查看 Client
的文档,您可以看到它的所有方法。 None 其中 post
.
相反,您需要使用 Client::request
并传入一个 Request
value. The constructor for Request
accepts a Method
,它表示要使用的 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| {
// ...
});
}
If just starting out, check out the Guides first.
有适合您的情况的指南:Advanced Client Usage。
我想使用 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]: 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
错误消息没有任何秘密诡计。您收到错误 "no method named post
found for type hyper::Client
" 因为没有这样的方法.
如果您查看 Client
的文档,您可以看到它的所有方法。 None 其中 post
.
相反,您需要使用 Client::request
并传入一个 Request
value. The constructor for Request
accepts a Method
,它表示要使用的 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| {
// ...
});
}
If just starting out, check out the Guides first.
有适合您的情况的指南:Advanced Client Usage。