Reqwest 的 Client.post() 为 File.io API 返回 400 个错误请求
Reqwest's Client.post() returning 400 bad request for File.io API
我正在学习 Rust,并认为构建一个 CLI 来与 File.io API 共享文件会很方便。
为此,我尝试使用 reqwest to send a request as described in the 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 响应。也许 headers 有问题?我试过查看 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")
表示"just a text"。您应该按照 here
所述使用 ReqwestBuilder::form
我正在学习 Rust,并认为构建一个 CLI 来与 File.io API 共享文件会很方便。
为此,我尝试使用 reqwest to send a request as described in the 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 响应。也许 headers 有问题?我试过查看 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")
表示"just a text"。您应该按照 here
所述使用ReqwestBuilder::form