1
0
mirror of https://github.com/nushell/nushell.git synced 2025-04-01 11:46:20 +02:00

Fix unwraps in post

This commit is contained in:
Jonathan Turner 2019-09-01 18:44:56 +12:00
parent dd3c149615
commit a7e378d1c9

View File

@ -151,25 +151,31 @@ pub async fn post(
let registry = registry.clone(); let registry = registry.clone();
let raw_args = raw_args.clone(); let raw_args = raw_args.clone();
if location.starts_with("http:") || location.starts_with("https:") { if location.starts_with("http:") || location.starts_with("https:") {
let login = encode(&format!("{}:{}", user.unwrap(), password.unwrap())); let login = match (user, password) {
(Some(user), Some(password)) => Some(encode(&format!("{}:{}", user, password))),
(Some(user), _) => Some(encode(&format!("{}:", user))),
_ => None,
};
let response = match body { let response = match body {
Tagged { Tagged {
item: Value::Primitive(Primitive::String(body_str)), item: Value::Primitive(Primitive::String(body_str)),
.. ..
} => { } => {
surf::post(location) let mut s = surf::post(location).body_string(body_str.to_string());
.body_string(body_str.to_string()) if let Some(login) = login {
.set_header("Authorization", format!("Basic {}", login)) s = s.set_header("Authorization", format!("Basic {}", login));
.await }
s.await
} }
Tagged { Tagged {
item: Value::Binary(b), item: Value::Binary(b),
.. ..
} => { } => {
surf::post(location) let mut s = surf::post(location).body_bytes(b);
.body_bytes(b) if let Some(login) = login {
.set_header("Authorization", format!("Basic {}", login)) s = s.set_header("Authorization", format!("Basic {}", login));
.await }
s.await
} }
Tagged { item, tag } => { Tagged { item, tag } => {
if let Some(converter) = registry.get_command("to-json") { if let Some(converter) = registry.get_command("to-json") {
@ -211,10 +217,13 @@ pub async fn post(
} }
} }
} }
surf::post(location)
.body_string(result_string) let mut s = surf::post(location).body_string(result_string);
.set_header("Authorization", format!("Basic {}", login))
.await if let Some(login) = login {
s = s.set_header("Authorization", format!("Basic {}", login));
}
s.await
} else { } else {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Could not automatically convert table", "Could not automatically convert table",