Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ reqwest = { version = "0.12", features = ["cookies"] }
cookie = "0.18"
tokio = { version = "1.48", features = ["full"] }
colored = "3.0"
serde = "1.0"
serde_json = "1.0"
serde_json5 = "0.2"
config-file = "0.2"
47 changes: 36 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::sync::Arc;
use std::{path::PathBuf, sync::Arc};

use clap::{Parser, ValueEnum};
use colored::*;
use config_file::FromConfigFile;
use reqwest::{
ClientBuilder, Request,
header::{HeaderName, HeaderValue},
};
use serde::Deserialize;

#[derive(Debug, Copy, Clone, ValueEnum)]
#[derive(Debug, Default, Copy, Clone, ValueEnum, Deserialize)]
enum Method {
#[default]
GET,
POST,
PUT,
Expand Down Expand Up @@ -52,21 +55,23 @@ impl From<Method> for reqwest::Method {
}
}

#[derive(Parser, Debug)]
#[derive(Parser, Debug, Deserialize)]
#[command(version, about)]
struct Cli {
/// The URL to request
url: String,
url: Option<String>,

#[arg(short, long, default_value_t = Method::GET)]
method: Method,
#[arg(short, long)]
method: Option<Method>,

/// Add a header to the request
#[arg(short = 'H', long = "header", value_name = "NAME=VALUE")]
#[serde(default)]
headers: Vec<String>,

/// Add a cookie to the request
#[arg(short = 'c', long = "cookie", value_name = "NAME=VALUE")]
#[serde(default)]
cookies: Vec<String>,

/// Short hand notation for the `Authorization` header
Expand All @@ -79,7 +84,13 @@ struct Cli {

/// Forces the body to be valid JSON
#[arg(short = 'j', long = "json-body")]
body_is_json: bool,
json_body: Option<bool>,

/// Path to a TOML configuration file.
/// Configuration from the command-line takes precedence.
#[arg(long = "config")]
#[serde(skip)]
config_file: Option<PathBuf>,
}

#[tokio::main]
Expand Down Expand Up @@ -108,9 +119,23 @@ impl std::fmt::Display for Error {
impl std::error::Error for Error {}

async fn run() -> std::result::Result<(), Box<dyn std::error::Error>> {
let args = Cli::parse();
let mut args = Cli::parse();

if let Some(config_file) = args.config_file {
let mut config = Cli::from_config_file(config_file)?;

args.url = args.url.or(config.url);
args.method = args.method.or(config.method);
args.headers.append(&mut config.headers);
args.cookies.append(&mut config.cookies);
args.auth = args.auth.or(config.auth);
args.body = args.body.or(config.body);
args.json_body = args.json_body.or(config.json_body);
}

let Some(url) = args.url else { return Ok(()) };

let url = reqwest::Url::parse(&args.url)?;
let url = reqwest::Url::parse(&url)?;

let jar = reqwest::cookie::Jar::default();
for cookie in &args.cookies {
Expand All @@ -121,7 +146,7 @@ async fn run() -> std::result::Result<(), Box<dyn std::error::Error>> {
.cookie_provider(Arc::new(jar))
.build()?;

let method = args.method.into();
let method = args.method.unwrap_or_default().into();

let mut request = Request::new(method, url);

Expand All @@ -143,7 +168,7 @@ async fn run() -> std::result::Result<(), Box<dyn std::error::Error>> {
}

if let Some(body) = args.body {
if args.body_is_json {
if args.json_body.unwrap_or_default() {
if let Err(err) = serde_json5::from_str::<serde_json::Value>(&body) {
return Err(Box::new(Error::InvalidJson(err)));
}
Expand Down