1

我正在学习 Rust,并认为构建一个 CLI 来与 File.io API 共享文件会很方便。

为此,我尝试使用reqwest发送请求,如File.io 文档中所述:

# from file.io doc -> works fine
$ curl --data "text=this is a secret pw" https://file.io
> {"success":true,"key":"zX0Vko","link":"https://file.io/zX0Vko","expiry":"14 days"}

当我运行下面的代码时,我得到一个 400 响应。也许标题有问题?我试过查看 curl 文档以找出我可能遗漏的内容,但我很难过。

任何帮助,将不胜感激。

我的代码:

extern crate reqwest;

fn main() {
    let client = reqwest::Client::new();
    let res = client.post("https://file.io/")
        .body("text=this is a practice run")
        .send();

    println!("{:?}", res);
}

预期反应:

{"success":true,"key":"SOME_KEY","link":"SOME_LINK","expiry":"14 days"}

实际反应:

Ok(Response { url: "https://file.io/", status: 400, headers: {"date": "Wed, 06 Feb 2019 03:40:35 GMT", "content-type": "application/json; charset=utf-8", "content-length": "64", "connection": "keep-alive", "x-powered-by": "Express", "x-ratelimit-limit": "5", "x-ratelimit-remaining": "4", "access-control-allow-origin": "*", "access-control-allow-headers": "Cache-Control,X-reqed-With,x-requested-with", "etag": "W/\"40-SEaBd3tIA9c06hg3p17dhWTvFz0\""} })
4

1 回答 1

2

您的要求不等价。curl --data表示您正在尝试发送内容类型为“x-www-form-urlencoded”或类似内容的 HTML 表单,而代码中的这一行

.body("text=this is a practice run")

意思是“只是一个文本”。您应该ReqwestBuilder::form按照此处所述使用

于 2019-02-06T05:13:41.090 回答