Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for HTTPie's --raw flag #202

Merged
merged 9 commits into from
Nov 30, 2021
Merged
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
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ default-features = false
features = ["parsing", "html", "yaml-load", "dump-load", "dump-create", "regex-onig"]

[dev-dependencies]
assert_cmd = "1.0"
assert_cmd = "2.0"
assert_matches = "1.4.0"
form_urlencoded = "1.0.1"
indoc = "1.0"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ OPTIONS:
-j, --json (default) Serialize data items from the command line as a JSON object
-f, --form Serialize data items from the command line as form fields
-m, --multipart Like --form, but force a multipart/form-data request even without files
--raw <RAW> Pass raw request data without extra processing
--pretty <STYLE> Controls output processing [possible values: all, colors, format, none]
-s, --style <THEME> Output coloring style [possible values: auto, solarized, monokai]
--response-charset <ENCODING> Override the response encoding for terminal display purposes
Expand Down
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ pub struct Cli {
#[structopt(short = "m", long, overrides_with_all = &["json", "form"])]
pub multipart: bool,

/// Pass raw request data without extra processing.
#[structopt(long, value_name = "RAW")]
pub raw: Option<String>,

/// Controls output processing.
#[structopt(long, possible_values = &Pretty::variants(), case_insensitive = true, value_name = "STYLE")]
pub pretty: Option<Pretty>,
Expand Down Expand Up @@ -365,6 +369,7 @@ const NEGATION_FLAGS: &[&str] = &[
"--no-print",
"--no-proxy",
"--no-quiet",
"--no-raw",
"--no-response-charset",
"--no-response-mime",
"--no-session",
Expand Down
42 changes: 33 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,29 @@ fn run(args: Cli) -> Result<i32> {

let (mut headers, headers_to_unset) = args.request_items.headers()?;

let ignore_stdin = args.ignore_stdin || atty::is(Stream::Stdin) || test_pretend_term();
let use_stdin = !(args.ignore_stdin || atty::is(Stream::Stdin) || test_pretend_term());
let body_type = args.request_items.body_type;
let mut body = args.request_items.body()?;
if !ignore_stdin {
if !body.is_empty() {
if body.is_multipart() {
let body_from_request_items = args.request_items.body()?;
let has_request_items = !body_from_request_items.is_empty();

let body = match (use_stdin, args.raw, has_request_items) {
(true, None, false) => {
let mut buffer = Vec::new();
stdin().read_to_end(&mut buffer)?;
Body::Raw(buffer)
}
(false, Some(raw), false) => Body::Raw(raw.into_bytes()),
(false, None, _) => body_from_request_items,

(true, Some(_), _) => {
return Err(anyhow!(
"Request body from stdin and --raw cannot be mixed. \
Pass --ignore-stdin to ignore standard input."
))
}
(true, _, true) => {
if args.multipart {
// Multipart bodies are never "empty", so we can get here without request items
return Err(anyhow!("Cannot build a multipart request body from stdin"));
} else {
return Err(anyhow!(
Expand All @@ -114,10 +131,17 @@ fn run(args: Cli) -> Result<i32> {
));
}
}
let mut buffer = Vec::new();
stdin().read_to_end(&mut buffer)?;
body = Body::Raw(buffer);
}
(_, Some(_), true) => {
if args.multipart {
// Multipart bodies are never "empty", so we can get here without request items
return Err(anyhow!("Cannot build a multipart request body from --raw"));
} else {
return Err(anyhow!(
"Request body (from --raw) and request data (key=value) cannot be mixed."
));
}
}
};

let method = args.method.unwrap_or_else(|| body.pick_method());
let timeout = args.timeout.and_then(|t| t.as_duration());
Expand Down
6 changes: 1 addition & 5 deletions src/request_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ impl Body {
match self {
Body::Json(map) => map.is_empty(),
Body::Form(items) => items.is_empty(),
Body::Raw(data) => data.is_empty(),
// A multipart form without items isn't empty, and we can't read
// a body from stdin because it has to match the header, so we
// should never consider this "empty"
Expand All @@ -237,6 +236,7 @@ impl Body {
// but that behavior is useless so there's no need to match it
Body::Multipart(..) => false,
Body::File { .. } => false,
Body::Raw(..) => false,
}
}

Expand All @@ -247,10 +247,6 @@ impl Body {
Method::POST
}
}

pub fn is_multipart(&self) -> bool {
matches!(self, Body::Multipart(..))
}
}

impl RequestItems {
Expand Down
Loading