Skip to content
Open
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
36 changes: 34 additions & 2 deletions packages/hurl/src/http/curl_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ fn body_params(
// > (HTTP) This posts data exactly as specified with no extra processing whatsoever.
//
// In summary: if the payload is a file (@foo.bin), we must use --data-binary option in
// order to curl to not process the data sent.
let param = match request_spec.body {
// order to curl to not process the data sent. For text bodies starting with @, use
// --data-raw to avoid making curl interpret the body as a filename.
let param = match &request_spec.body {
Body::File(_, _) => "--data-binary",
Body::Text(s) if s.starts_with('@') => "--data-raw",
_ => "--data",
};
args.push(param.to_string());
Expand Down Expand Up @@ -1107,6 +1109,36 @@ mod tests {
);
}

#[test]
fn post_text_body_starting_with_at() {
let request = RequestSpec {
method: Method("POST".to_string()),
url: Url::from_str("http://localhost:8000/hello").unwrap(),
body: Body::Text("@filename\n".to_string()),
..Default::default()
};

let context_dir = ContextDir::default();
let cookie_store = CookieStore::new();
let options = ClientOptions::default();
let output = None;

let cmd = CurlCmd::new(
&request,
&cookie_store,
&context_dir,
output.as_ref(),
&options,
);
assert_eq!(
cmd.to_string(),
"curl \
--header 'Content-Type:' \
--data-raw $'@filename\\n' \
'http://localhost:8000/hello'"
);
}

#[test]
fn test_encode_byte() {
assert_eq!(encode_byte(1), "\\x01".to_string());
Expand Down
Loading