Curl Crate post and get data

Is there any way I can make a post request and get the data? This is what I am using right now but I am getting 65k 0s

            let mut dst = Vec::new();
            let mut easy = Easy::new();
            let mut data = "version".as_bytes();
            easy.url(&[json["Ip"].to_string().trim_matches('\"').to_string(), ":1754".to_string()].join("")).unwrap();
            easy.post(true).unwrap();
            easy.post_field_size(data.len() as u64).unwrap();
            let mut transfer = easy.transfer();
            transfer
                .read_function(|buf| {
                    dst.extend_from_slice(buf);
                    Ok(data.read(buf).unwrap_or(0))
                })
                .unwrap();

so I dont know if I am just doing it wrong or what. I also tried curling it normally from the terminal and it works so I have no idea.

I'm not familiar with the curl crate, but that read_function looks very wrong. It seems like you are attempting to both send your request and read the response in it?

That is what I want to do but I have no idea how do I do it, if you know a easier way in a different crate to do this I am all ears.

Try this:

transfer
    .read_function(|buf| {
        let len = std::cmp::min(buf.len(), data.len());
        buf[..len].copy_from_slice(&data[..len]);
        data = &data[len..];
        Ok(len)
    })
    .unwrap();
transfer
    .write_function(|buf| {
        dst.extend_from_slice(buf);
        Ok(buf.len())
    })
    .unwrap();
1 Like

thank you that worked

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.