trying to make multi form post data and struggling
This commit is contained in:
parent
4f58b1c1ee
commit
1cc4ca8661
@ -2,11 +2,10 @@
|
|||||||
name = "crunk-update"
|
name = "crunk-update"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["crunk <dvanderkleij@gmail.com>"]
|
authors = ["crunk <dvanderkleij@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.0", features = ["derive"] }
|
clap = { version = "4.0", features = ["derive"] }
|
||||||
reqwest = { version = "0.11", features = ["json"] }
|
reqwest = { version = "0.11.20", features = ["json", "blocking", "multipart"] }
|
||||||
tokio = { version = "1", features = ["full"] }
|
|
||||||
|
33
src/main.rs
33
src/main.rs
@ -1,6 +1,7 @@
|
|||||||
#![deny(warnings)]
|
#![deny(warnings)]
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use reqwest::blocking::multipart;
|
||||||
|
|
||||||
/// Search for a pattern in a file and display the lines that contain it.
|
/// Search for a pattern in a file and display the lines that contain it.
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
@ -9,27 +10,31 @@ use clap::Parser;
|
|||||||
#[command(version = "1.0")]
|
#[command(version = "1.0")]
|
||||||
#[command(about = "post updates to crunk-schedulers", long_about = None)]
|
#[command(about = "post updates to crunk-schedulers", long_about = None)]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
#[arg(long)]
|
#[arg(short, long)]
|
||||||
url: String,
|
url: String,
|
||||||
#[arg(long)]
|
#[arg(short, long, group = "input")]
|
||||||
text: String,
|
text: Option<String>,
|
||||||
|
#[arg(short, long, group = "input")]
|
||||||
|
file: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
#[tokio::main]
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
|
||||||
async fn main() -> Result<(), reqwest::Error> {
|
|
||||||
let args = Cli::parse();
|
let args = Cli::parse();
|
||||||
let url = args.url;
|
let url = args.url;
|
||||||
let text = args.text;
|
let text = args.text;
|
||||||
|
if text.is_some() {
|
||||||
|
let client = reqwest::blocking::Client::new();
|
||||||
|
let res = client.post(url).form(&[("text", text)]).send()?;
|
||||||
|
println!("{:?}", res);
|
||||||
|
}
|
||||||
|
if args.file.is_some() {
|
||||||
|
let file_name = args.file.as_deref().unwrap_or_default();
|
||||||
|
let client = reqwest::blocking::Client::new();
|
||||||
|
let form = multipart::Form::new().file("file", &file_name)?;
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let res = client.post("your url").multipart(form).send()?;
|
||||||
let res = client.post(url).form(&[("text", text)]).send().await?;
|
println!("{:?}", res)
|
||||||
|
}
|
||||||
eprintln!("Response: {:?} {}", res.version(), res.status());
|
|
||||||
eprintln!("Headers: {:#?}\n", res.headers());
|
|
||||||
|
|
||||||
let body = res.text().await?;
|
|
||||||
println!("{}", body);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user