How to transfer JSON file

My target is to transfer a JSON file from a client to an http server using reqwest crate.
I am using the following sample. The good news is, it complies and is working. But in this code the JSON is a handcrafted file in the code, where as my target is to transfer/post an already available file ( from local machine file system) to the http server. How to achieve this please.

use error_chain::error_chain;
use std::collections::HashMap;

error_chain! {
foreign_links {
HttpRequest(reqwest::Error);
IoError(::std::io::Error);
}
}
#[tokio::main]

async fn main() -> Result<()> {
let mut map = HashMap::new();

map.insert("FirsName", "Syed");
map.insert("LastName", "Kamran");

let client = reqwest::Client::new();
let res = client.post("https://paste.rs")
.json(&map)
.send()
.await?;

let response_text = res.text().await?;
println!("Your paste is located at: {}",response_text );   
Ok(())            

}

You may want to start by reading the filesystem library docs.

1 Like

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.