Reqwest 的 Client.post() 返回 400 个错误请求 File.io API



我正在学习 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""} })

您的请求不等效。 curl --data意味着您正在尝试发送内容类型为"x-www-form-urlencoded"或类似内容的HTML表单,而代码中的这一行

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

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

最新更新