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 3 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
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
37 changes: 29 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,25 @@ fn run(args: Cli) -> Result<i32> {

let ignore_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 body = match (!ignore_stdin, args.raw, !body_from_request_items.is_empty()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find matching on negated booleans hard to follow.

Maybe it would be better to turn ignore_stdin into let use_stdin = !(args.ignore_stdin || atty::is(Stream::Stdin) || test_pretend_term());? And perhaps let has_request_items = !body_from_request_items.is_empty();.

(true, None, false) => {
let mut buffer = Vec::new();
stdin().read_to_end(&mut buffer)?;
Body::Raw(buffer)
}
(false, Some(raw), false) => Body::Raw(raw.as_bytes().to_vec()),
ducaale marked this conversation as resolved.
Show resolved Hide resolved
(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 {
return Err(anyhow!("Cannot build a multipart request body from stdin"));
ducaale marked this conversation as resolved.
Show resolved Hide resolved
} else {
return Err(anyhow!(
Expand All @@ -114,10 +129,16 @@ 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 {
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
4 changes: 0 additions & 4 deletions src/request_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,6 @@ impl Body {
Method::POST
}
}

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

impl RequestItems {
Expand Down
33 changes: 33 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,30 @@ fn mixed_stdin_request_items() {
));
}

#[test]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also test successful use of the option?

fn mixed_stdin_raw() {
let input_file = tempfile().unwrap();
redirecting_command()
.args(&["--offline", "--raw=hello", ":"])
.stdin(input_file)
.assert()
.failure()
.stderr(contains(
"Request body from stdin and --raw cannot be mixed",
));
}

#[test]
fn mixed_raw_request_items() {
get_command()
.args(&["--offline", "--raw=hello", ":", "x=3"])
.assert()
.failure()
.stderr(contains(
"Request body (from --raw) and request data (key=value) cannot be mixed",
));
}

#[test]
fn multipart_stdin() {
let input_file = tempfile().unwrap();
Expand All @@ -1361,6 +1385,15 @@ fn multipart_stdin() {
.stderr(contains("Cannot build a multipart request body from stdin"));
}

#[test]
fn multipart_raw() {
get_command()
.args(&["--offline", "--raw=hello", "--multipart", ":"])
.assert()
.failure()
.stderr(contains("Cannot build a multipart request body from --raw"));
}

#[test]
fn default_json_for_raw_body() {
let server = server::http(|req| async move {
Expand Down