Skip to content

Commit 1a57b9f

Browse files
committed
allow body to be read from file with @
1 parent 08ce2c6 commit 1a57b9f

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/main.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ struct Cli {
9191
#[command(flatten)]
9292
auth_type: AuthType,
9393

94-
/// Add body contents.
94+
/// Add body contents (prefix with @ to read from file).
9595
#[arg(short = 'b', long = "body")]
9696
bodies: Vec<String>,
9797

@@ -228,9 +228,18 @@ async fn run() -> std::result::Result<(), Box<dyn std::error::Error>> {
228228
request = request.header(CONTENT_TYPE, "application/json");
229229
}
230230

231+
let mut bodies = vec![];
232+
for body in args.bodies {
233+
bodies.push(if let Some(file_path) = body.strip_prefix('@') {
234+
std::fs::read_to_string(file_path)?
235+
} else {
236+
body
237+
});
238+
}
239+
231240
if args.body_type.form {
232241
let mut form = reqwest::multipart::Form::new();
233-
for field in &args.bodies {
242+
for field in bodies {
234243
let (key, value) = field
235244
.split_once('=')
236245
.ok_or_else(|| Error::InvalidFormField(field.clone()))?;
@@ -239,7 +248,7 @@ async fn run() -> std::result::Result<(), Box<dyn std::error::Error>> {
239248
request = request.multipart(form);
240249
} else {
241250
let mut concatenated_body = String::new();
242-
for body in args.bodies {
251+
for body in bodies {
243252
if args.body_type.json {
244253
if let Err(err) = serde_json5::from_str::<serde_json::Value>(&body) {
245254
return Err(Box::new(Error::InvalidJson(err)));

0 commit comments

Comments
 (0)