我想用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();
}
Run Code Online (Sandbox Code Playgroud)
错误:
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
Run Code Online (Sandbox Code Playgroud)
错误消息没有秘密的诡计.您收到错误"没有post为类型找到的方法hyper::Client",因为没有这样的方法.
如果查看文档Client,可以看到它的所有方法.他们都不是post.
相反,您需要使用Client::request并传入一个Request值.Request接受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| {
// ...
});
}
Run Code Online (Sandbox Code Playgroud)
该箱的文件说:
如果刚刚起步,检查出的指南第一.
确切地说明了您的案例指南:高级客户端使用.
| 归档时间: |
|
| 查看次数: |
624 次 |
| 最近记录: |