I wanted to post a json file to an http server. I am using following code to achieve this functionality. I wonder, if some one can help me figure out, what may be the possible test cases in this case please.
Code to post a Json file to a http server using serde_json and reqwest
use error_chain::error_chain;
use serde_json:: {Value};
use std::env;
use std::fs;
error_chain! {
foreign_links {
HttpRequest(reqwest::Error);
IoError(::std::io::Error);
}
}
#[tokio::main]
async fn main() -> Result<()> {
//Get file name from command line argument
let args: Vec<String> = env::args().collect();
let file_name = &args[1];
println!("The file name is: {}", file_name);
//First load the file into a string
let text = fs::read_to_string(file_name)?;
println!("The text of the file is {}", text);
//Parse the string into a dynamically-typed JSON structure.
let json_file= serde_json::from_str::<Value>(&text).unwrap();
//Post the file to an http server
let client = reqwest::Client::new();
let res = client.post("https://paste.rs")
.json(&json_file)
.send()
.await?;
//Get and print responce values
let response_text = res.text().await?;
println!("Your paste is located at: {}",response_text );
Ok(())
}