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

哈里森·格里夫

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

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

# 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所述使用

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章