#'static headaches with reqwest

4 messages · Page 1 of 1 (latest)

elfin dew
#

And the relevant body of the method looks like


let mut form = reqwest::multipart::Form::new();
                let payload_json = to_string(message).unwrap();
                let mut payload_field =
                    reqwest::multipart::Part::text(payload_json).file_name("payload_json");
                payload_field = match payload_field.mime_str("application/json") {
                    Ok(part) => part,
                    Err(e) => {
                        return Err(InstanceServerError::MultipartCreationError {
                            error: e.to_string(),
                        })
                    }
                };

                form = form.part("payload_json", payload_field);

                for (index, attachment) in files.iter().enumerate() {
                    let part_name = format!("files[{}]", index);
                    let content_disposition = format!(
                        "form-data; name=\"{}\"'; filename=\"{}\"",
                        part_name,
                        attachment
                            .get(index)
                            .unwrap()
                            .filename
                            .as_deref()
                            .unwrap_or("file")
                    );
                    let mut header_map = HeaderMap::new();
                    header_map
                        .insert(CONTENT_DISPOSITION, content_disposition.parse().unwrap())
                        .unwrap();

                 
#
let mut part =
                        multipart::Part::bytes(attachment.get(index).unwrap().content.as_slice())
                            .file_name(
                                attachment
                                    .get(index)
                                    .unwrap()
                                    .filename
                                    .as_deref()
                                    .unwrap_or("file"),
                            )
                            .headers(header_map);

                    part = match part.mime_str("application/octet-stream") {
                        Ok(part) => part,
                        Err(e) => {
                            return Err(InstanceServerError::MultipartCreationError {
                                error: e.to_string(),
                            })
                        }
                    };

                    form = form.part(part_name, part);
#

I am getting the error that ˋfilesˋ does not live long enough and does not fulfill that static requirement, which is imposed by Multipart::Part::bytes() and Multipart::Part::file_name()

If you’d like to really look at everything in-depth, the code is available with

ˋgit clone https://github.com/polyphony-chat/chorus.gitˋ, or simply at https://github.com/polyphony-chat/chorus

GitHub

A rust library for interacting with multiple Discord.com-compatible APIs and Gateways at the same time. - GitHub - polyphony-chat/chorus: A rust library for interacting with multiple Discord.com-co...

#

Had to split this in three because of discords message length requirements