Harrison Grieve
Harrison Grieve

Reputation: 13

Reqwest's Client.post() returning 400 bad request for File.io API

I'm learning Rust, and thought it would be handy to build a CLI to share files with the File.io API.

To do so, I am trying to use 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"}

When I run the below code, I get a 400 response. Perhaps there's an issue with the headers? I've tried looking at the curl docs to find out what I could be missing, but I'm stumped.

Any help would be appreciated.

My code:

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);
}

Expected Response:

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

Actual Response:

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\""} })

Upvotes: 1

Views: 1127

Answers (1)

Laney
Laney

Reputation: 1649

Your requests are not equivalent. curl --data means you're trying to send a HTML form with content type "x-www-form-urlencoded" or similar, whereas this line in your code

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

means "just a text". You should use ReqwestBuilder::form as described here

Upvotes: 2

Related Questions