Code not sending data over HTTP asynchronously

I am working on a rust application which will be sending data to server via HTTP. However the requests are being sent synchronously i.e before sending next request it waits for previous response. What I need to correct to send data asynchronously :-

pub struct Connector {
    client: reqwest::r#async::Client,
}

impl Connector {
    pub fn new() -> Self {
        let client = reqwest::r#async::Client::new();
        Self {
            client,
        }
    }

    pub fn send(&self, data: &PersonalData) -> impl Future<Item = (), Error = ()> {
		
		let url = "http://localhost:9000/post";
		println!("----DATA----");
		println!("{:?}", data);
		
		self.client.post(url)
				.json(&data)
				.send()
				.map(|response| debug!("HTTP response: {:?}", response))
				.map_err(|error| warn!("HTTP error: {:?}", error))
    }
}

In the main code, on event trigger, I parse the URL out of event and pass this future to executor.

    let connector = Arc::new(// connector instance);.
    while (data) {  // Event loop receiving data continuously.
        let connector = connector.clone();
        tokio::spawn(connector.send(data));
    }
   

Do you try create several clients (reqwest::r#async::Client) ?

AFAIK, HTTP by default follows simple request/response protocol. With HTTP 1.1 Pipelining was introduced with some limitations, HTTP 2.0 now supports async. request/response.

I am not sure reqwest supports HTTP 2.0 for async request/response.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.