如何从Rust发出HTTP请求? [英] How do I make an HTTP request from Rust?

查看:1383
本文介绍了如何从Rust发出HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Rust发出HTTP请求?我似乎无法在核心库中找到任何内容。



我不需要解析输出,只需发出请求并检查HTTP响应代码。 / p>

如果有人可以告诉我如何对我的网址上的查询参数进行URL编码,则可以获得奖励!

解决方案

更新:这个答案指的是相当古老的历史。有关当前的最佳做法,请查看 Isaac Aggrey的回答






我一直致力于生锈 - http ,它已经成为Rust的事实上的 HTTP库(Servo使用它);它远非完整,目前记录非常糟糕。以下是使用状态代码发出请求并执行操作的示例:

  extern mod http; 
使用http :: client :: RequestWriter;
使用http :: method :: Get;
使用http :: status;
使用std :: os;

fn main(){
let request = RequestWriter :: new(Get,FromStr :: from_str(os :: args()[1])。unwrap());
let response = match request.read_response(){
Ok(response)=>响应,
Err(_request)=>无法到达!(),//未捕获的条件将首先失败
};
如果response.status == status :: Ok {
println!(哦,好吧,我给了我一个200 OK响应!);
} else {
println!(该URL未返回200 OK,它返回{}而不是,response.status);
}
}

使用URL作为唯一命令运行此代码 - 行参数,它将检查状态代码! (仅限HTTP;无HTTPS。)



src / examples / client / client.rs 比较示例那会多一点。



rust-http正在跟踪生锈的主分支。 目前它将在刚刚发布的Rust 0.8中运行,但很快就会出现重大变化。 实际上,没有版本的rust-http可以在Rust 0.8上运行 - 有一个在发布之前,在隐私规则中无法解决的更改,留下了一些生锈的http取决于extra :: url不可访问。这已经修复了,但它留下了锈-http与Rust 0.8不兼容。






至于查询字符串编码问题,目前应该使用 extra :: url :: Query 〜[(~str,~str)]> )。转换的适当函数:




How can I make an HTTP request from Rust? I can't seem to find anything in the core library.

I don't need to parse the output, just make a request and check the HTTP response code.

Bonus marks if someone can show me how to URL encode the query parameters on my URL!

解决方案

Update: This answer refers to fairly ancient history. For the current best practices, please look at Isaac Aggrey's answer instead.


I've been working on rust-http, which has become the de facto HTTP library for Rust (Servo uses it); it's far from complete and very poorly documented at present. Here's an example of making a request and doing something with the status code:

extern mod http;
use http::client::RequestWriter;
use http::method::Get;
use http::status;
use std::os;

fn main() {
    let request = RequestWriter::new(Get, FromStr::from_str(os::args()[1]).unwrap());
    let response = match request.read_response() {
        Ok(response) => response,
        Err(_request) => unreachable!(), // Uncaught condition will have failed first
    };
    if response.status == status::Ok {
        println!("Oh goodie, I got me a 200 OK response!");
    } else {
        println!("That URL ain't returning 200 OK, it returned {} instead", response.status);
    }
}

Run this code with a URL as the sole command-line argument and it'll check the status code! (HTTP only; no HTTPS.)

Compare with src/examples/client/client.rs for an example that does a little more.

rust-http is tracking the master branch of rust. At present it'll work in the just-released Rust 0.8, but there are likely to be breaking changes soon. Actually, no version of rust-http works on Rust 0.8—there was a breaking change which can't be worked around in privacy rules just before the release, leaving something that rust-http depends on in extra::url inaccessible. This has since been fixed, but it leaves rust-http incompatible with Rust 0.8.


As for the query string encoding matter, at present that should be done with extra::url::Query (a typedef for ~[(~str, ~str)]). Appropriate functions for conversions:

这篇关于如何从Rust发出HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆