1

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

1 Answers1

2

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

Laney
  • 1,571
  • 9
  • 7
  • Thanks for the help! It's working as expected. If I wanted to extend this to uploading a file (see my [python implementation](https://github.com/rhgrieve/fastball/blob/master/fastball.py)), would I be able to accomplish this with a HashMap? Error output states `the trait serde::ser::Serialize is not implemented for std::fs::File`. Is this an issue with the collection itself? – Harrison Grieve Feb 06 '19 at 10:36
  • according to documentation `files=` param in python `requests` means "send multipart-encoded file". Corresponding function in `reqwest`: https://docs.rs/reqwest/0.9.9/reqwest/struct.RequestBuilder.html#method.multipart – Laney Feb 06 '19 at 12:54