Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any example of multipart post? #120

Closed
elasmojs opened this issue Jul 23, 2020 · 7 comments
Closed

Any example of multipart post? #120

elasmojs opened this issue Jul 23, 2020 · 7 comments

Comments

@elasmojs
Copy link

Hi, Thanks to ureq, have achieved what I wanted to a large extent in my project. Is there any multipart suppor/example right now?

@algesten
Copy link
Owner

@elasmojs there isn't any built in support for multipart forms. Constructing the multipart is easy enough though – I was briefly looking through crates.io, but can't find a framework agnostic impl. Could be I didn't look hard enough, or that that someone really ought to build a generic multipart builder.

I think the API should look something like this:

let multi = MultipartBuilder::new()
  .addText("Some text") // Content-Disposition: form-data; name="text"
  .addFile("myfile.txt") // Content-Disposition: form-data; name="file1"; filename="myfile.txt"
  .build().unwrap(); // delay io errors to here

let res = ureq::post("https://my-fine.server")
  .set("content-length", multi.len())
  .set("content-type", multi.content_type()) // multipart/form-data; boundary=----90519140972754266
  .send(multi.into_reader());

@elasmojs
Copy link
Author

elasmojs commented Aug 7, 2020

@algesten , thank you for the response. I have solved this by using https://crates.io/crates/multipart

@murlakatamenka
Copy link

@elasmojs can you provide an example, please?
I'm not so familiar with web stuff, so I ended up switching to reqwest since it supports multipart forms out of the box. Just curious how it can be achieved with ureq + multipart.

@jhwgh1968
Copy link

I came across this too, and also implemented it myself.

To aid any future visitors, I have written up a gist which should serve as a good template:
https://gist.github.com/jhwgh1968/3cd073677f74506474bbcbcadeffb0ee

@flyInRust
Copy link

i implemented a multipart toolkit for ureq:
ureq_multipart

usage:

`
use ureq_multipart::MultipartBuilder;

let (content_type,data) = MultipartBuilder::new()
.add_file("test","/home/feiy/Desktop/1.txt").unwrap()
.finish().unwrap();
let resp: Value = req
.post(&service_url)
.set("Content-Type", &content_type)
.send_bytes(&data)?
.into_json()?
`

@flyInRust
Copy link

i have public ureq_multipart 1.1 ,add ureq request send multipart method:

use ureq_multipart::MultipartRequest;

let resp: Value = req
            .post(&service_url)
            .send_multipart_file("name","&/home/feiy/Desktop/1.txt")?
            .into_json()?

@messense
Copy link
Contributor

messense commented Nov 18, 2023

FYI, the multipart crate is unmaintained: https://rustsec.org/advisories/RUSTSEC-2023-0050.html

I'd love to see ureq to gain builtin support for multipart so we don't need to depend on an unmaintained crate.

See also PyO3/maturin#1858

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants