Parse multipart/form-data response in rust / reqwest

I'm relatively new to rust and using reqwest to fetch a PDF document from a REST API endpoint. The content-type of the response is multipart/form-data; boundary=bc1f6465-6738-4b46-9d9d-b9ae36afa8cb with two parts:

--bc1f6465-6738-4b46-9d9d-b9ae36afa8cb
Content-Disposition: form-data; name="metadata"
Content-Type: application/json

{"documentId":"QkNfRENfSUwwMDEsRTA1OEU3ODQtMDAwMC1DNzY5LTg1MjktMTRFRkI5RTBFNjRF"}
--bc1f6465-6738-4b46-9d9d-b9ae36afa8cb
Content-Disposition: form-data; name="document"; filename=document.pdf
Content-Type: application/pdf

%PDF-1.4
<binary content>
%%EOF

--bc1f6465-6738-4b46-9d9d-b9ae36afa8cb--

I want now to save the PDF document in the 2nd part as a valid PDF file on disk. But the multipart functionality within reqwest seems to create new multipart requests whereas I need to parse a multipart response.

My code to download the file:

use reqwest::{self, header::AUTHORIZATION};
fn main() {
    let url = "https://example.com/rest/document/123";
    let authorization_header = String::from("Bearer ") + access_token.as_str();
    let res = client.get(url)
        .header(AUTHORIZATION, &authorization_header)
        .send()
        .expect("Error calling API");
}

Any hint on how to process the multipart/form-data response is appreciated!

I'm not sure I've ever seen an API endpoint that returned multipart form data, that's interesting!

I think you could use the multipart crate's server module to parse the body, via Multipart::with_body

Indeed, quite a rare scenario. Having spoken about the lack of library support (not only in rust) for multipart-responses the developer changed the content-type to application/pdf.
Thanks for pointing me to the multipart crate, will use that for the next project :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.