alexcrichton / curl-rust

Rust bindings to libcurl
MIT License
1k stars 234 forks source link

Reqest example of using multiform post call for begginers. #463

Open djve60 opened 1 year ago

djve60 commented 1 year ago

I'm a rust newbie so I'm having problems figuring out how to populate a multipart form.

_.... client.url(&uri).unwrap(); let mut form = Form::new(); <how to add a the data so it's a key:value pair>

client.httppost(form).unwrap();_

I've tried form.part("loggingin").add(); but nothing is being added to the form.

https://docs.rs/curl/0.4.44/curl/easy/struct.Easy2.html#method.httppost points me to https://docs.rs/curl/0.4.44/curl/easy/struct.Form.html and then I hit the wall in confusion.

I suspect adding a simple example to the examples directory would be all most people need for a guide. Just something with a key of "Foo" and a value of "Bar" is all I'm after. Like the header example, but that uses List, not Form.

Looking at the tests directory I tried: form.part("loggingin").contents(b"Yup.").add();

But it doesn't look like it's done anything as println!("Form {:#?}", form); returns Form Form { fields: "...", }

sagebind commented 1 year ago

Generally speaking the official libcurl examples are useful for learning how to use libcurl, and since curl-rust is just a fairly thin wrapper around libcurl most of the examples should translate from C to Rust to some degree. If you are a beginner, then curl-rust is definitely not the easiest HTTP client to get started with (unless you are already familiar with libcurl).

For your specific questions, this is indeed the correct syntax:

// Make a new form
let mut form = Form::new();

form.part("field1") // define a field named `field1`
    .contents(b"foo") // with a value
    .add().unwrap(); // and finish adding the key-value pair to the form

// Use the form in the request
easy.httppost(form).unwrap();

But it doesn't look like it's done anything as println!("Form {:#?}", form); returns Form Form { fields: "...", }

I would not rely on the Debug implementation to provide very much useful information, especially with structs that are wrapping foreign C types. You'd have to send the request and verify whether it works.

djve60 commented 1 year ago

Many thanks, I'm a newbie to rust. I've used libcurl in C, Python, and Perl, and of course the CLI version for quick debugging. I was missing the call to unwrap() after the add(). At least I understand that now it was a wrapped value, still having issues about how often I need to use unwrap(), but it's fun and a learning curve.

I went to curllib in rust as it follows redirects, when configured. The reqwests module will not, and hyper will but you need to manually set it up. So it's back to curllib, for me. :-)

Now I just have to figure out how to use transfer.read_function() to be the body, as raw data from a String or str type. This would be the equivalent of "--raw-data" in the command line versions. Lots of examples for bytes, that I've got running but converting this to a simple body... I'm sure it's in the docs but I'm just not seeing it. And by docs I mean docs.rs and the gitbub repository directories of examples, and test.