Curl Post request with XML input

Hello there,

I'm very new to rust, i want to try this langage.

I want to do a curl post request but in my POST i send an xml (that i import with a file) to the apache tomcat server, which in return needs to give me back a response. For yet it does give me a response but i think it's because the Xml that i send it not encoded properly. I tried different methods to import it but everytime it says it doesn't have a proper read methods. So i tried to convert it with as_bytes but it gives me this error :

    <!doctype html><html lang="fr"><head><title>État HTTP 500 – Erreur interne du serveur</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>État HTTP 500 – Erreur interne du serveur</h1><hr class="line" /><p><b>Type</b> Rapport d'état</p><p><b>message</b> Error executing job: Error reading log file: &lt;?xml version=&quot;1.0&quot;?&gt;
&lt;error component=&quot;XmlUtils&quot; desc=&quot;XML parsing error. See Message for details.&quot;

here is my code :

    let contents = fs::read_to_string(filename)

            .expect("Something went wrong reading the file");

        let mut contentsbyte = contents.as_bytes(); 

        println!("With text:\n{}", contents);

        let mut dataread = String::new();

        let f = File::open(filename).expect("Unable to open file");

        let mut br = BufReader::new(f);

        br.read_to_string(&mut dataread).expect("Unable to read string");

        println!("{}", dataread);

        let mut datareadr = dataread.as_bytes();

        let mut easy = Easy::new();

        easy.url(lxmlsrv_url).unwrap();

        easy.post(true).unwrap();

        let mut transfer = easy.transfer();

        transfer.read_function(|into| {

            Ok(contentsbyte.read(into).unwrap())

        }).unwrap();

        transfer.write_function(|data| {

            // "data!" is never printed

            println!("data!");

            stdout().flush().unwrap();

            Ok(stdout().write(data).unwrap())

        });

        transfer.perform().unwrap();
    ```






> 
> So, is there any methods/functions to send/import a file with the right encoding ? Thanks in advance !

I’m not familiar with the rust curl bindings. It’s not obvious to me if it’s an issue with using the bindings or an issue with your data. To rule out the second option does running the following curl command from the command line succeed (filling in the name of the file with the xml data and the url)?

curl -X POST -d @myfilename http://user:pass@myhost:myport/path/of/url

If that is failing then the server has an issue with your xml data, and you should resolve that before trying to get the rust code to work.

Another suggestion is that it might be easier to use a native rust library such as ureq (or reqwest) rather than bindings to libcurl.

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.