From dbb30cc9e033cd1a8505cfd172c26571a4113958 Mon Sep 17 00:00:00 2001 From: Noah Date: Sat, 24 May 2025 19:53:59 +0200 Subject: [PATCH] feat: default http protocol when none used in http request (#15804) https://github.com/nushell/nushell/issues/10957 Hello, this PR proposes a solution for some requested features mentioned in https://github.com/nushell/nushell/issues/10957. I personally think these are very simple changes that bring significant quality of life improvements. It gives the possibility to do `http get google.com` instead of `http get http://google.com` and `http get :8080` instead of `http get http://localhost:8080`. I did not address the other part of the issue (data management) as those are more controversial. --- crates/nu-command/src/network/http/client.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/src/network/http/client.rs b/crates/nu-command/src/network/http/client.rs index 7efbd86512..b932f734bd 100644 --- a/crates/nu-command/src/network/http/client.rs +++ b/crates/nu-command/src/network/http/client.rs @@ -78,12 +78,22 @@ pub fn http_parse_url( span: Span, raw_url: Value, ) -> Result<(String, Url), ShellError> { - let requested_url = raw_url.coerce_into_string()?; + let mut requested_url = raw_url.coerce_into_string()?; + if requested_url.starts_with(':') { + requested_url = format!("http://localhost{}", requested_url); + } else if !requested_url.contains("://") { + requested_url = format!("http://{}", requested_url); + } + let url = match url::Url::parse(&requested_url) { Ok(u) => u, Err(_e) => { - return Err(ShellError::UnsupportedInput { msg: "Incomplete or incorrect URL. Expected a full URL, e.g., https://www.example.com" - .to_string(), input: format!("value: '{requested_url:?}'"), msg_span: call.head, input_span: span }); + return Err(ShellError::UnsupportedInput { + msg: "Incomplete or incorrect URL. Expected a full URL, e.g., https://www.example.com".to_string(), + input: format!("value: '{requested_url:?}'"), + msg_span: call.head, + input_span: span + }); } };