Skip to content

Commit bdbe32c

Browse files
cyphercodeshurl-bot
authored andcommitted
Fix curl command for text bodies starting with at sign
1 parent 76caed6 commit bdbe32c

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

packages/hurl/src/http/curl_cmd.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ fn body_params(request_spec: &RequestSpec, context_dir: &ContextDir) -> Vec<Stri
192192
// > (HTTP) This posts data exactly as specified with no extra processing whatsoever.
193193
//
194194
// In summary: if the payload is a file (@foo.bin), we must use --data-binary option in
195-
// order to curl to not process the data sent.
196-
let param = match request_spec.body {
195+
// order to curl to not process the data sent. For text bodies starting with @, use
196+
// --data-raw to avoid making curl interpret the body as a filename.
197+
let param = match &request_spec.body {
197198
Body::File(_, _) => "--data-binary",
199+
Body::Text(s) if s.starts_with('@') => "--data-raw",
198200
_ => "--data",
199201
};
200202
args.push(param.to_string());
@@ -1016,6 +1018,36 @@ mod tests {
10161018
);
10171019
}
10181020

1021+
#[test]
1022+
fn post_text_body_starting_with_at() {
1023+
let request = RequestSpec {
1024+
method: Method("POST".to_string()),
1025+
url: Url::from_str("http://localhost:8000/hello").unwrap(),
1026+
body: Body::Text("@filename\n".to_string()),
1027+
..Default::default()
1028+
};
1029+
1030+
let context_dir = ContextDir::default();
1031+
let cookie_store = CookieStore::new();
1032+
let options = ClientOptions::default();
1033+
let output = None;
1034+
1035+
let cmd = CurlCmd::new(
1036+
&request,
1037+
&cookie_store,
1038+
&context_dir,
1039+
output.as_ref(),
1040+
&options,
1041+
);
1042+
assert_eq!(
1043+
cmd.to_string(),
1044+
"curl \
1045+
--header 'Content-Type:' \
1046+
--data-raw $'@filename\\n' \
1047+
'http://localhost:8000/hello'"
1048+
);
1049+
}
1050+
10191051
#[test]
10201052
fn test_encode_byte() {
10211053
assert_eq!(encode_byte(1), "\\x01".to_string());

0 commit comments

Comments
 (0)