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

问题描述 投票:1回答:2

我正在学习锈,并认为这将是很方便的建立一个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响应。也许有一个与头的问题?我试着看袅袅的文档,找出我可能是错过了什么,但我难倒。

任何帮助,将不胜感激。

我的代码:

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\""} })
rust reqwest
2个回答
2
投票

您的要求是不等价的。 curl --data意味着你要发送HTML表单的内容类型为“X WWW的形式,进行了urlencoded”或类似的,而这一行代码

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

意思是“只是一个文本”。您应该使用ReqwestBuilder::form描述here

© www.soinside.com 2019 - 2024. All rights reserved.