net/http.Request.URL.Host returns empty string












1















I was trying to redirect my client to https url. And I tried this:



func index(w http.ResponseWriter, r *http.Request) {
if r.URL.Scheme != "https" {
http.Redirect(w, r, "https://"+r.URL.Host+r.URL.Path, 301)
return
}
//....
}


But it gave me this response:



$ curl -i http://localhost
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: https:///
Date: Sat, 24 Nov 2018 20:02:33 GMT
Content-Length: 44

<a href="https:///">Moved Permanently</a>.


The mysterious thing is Location: https:/// this line. And I read the go doc again and found:




// URL specifies either the URI being requested (for server
// requests) or the URL to access (for client requests).
//
// For server requests the URL is parsed from the URI
// supplied on the Request-Line as stored in RequestURI. **For
// most requests, fields other than Path and RawQuery will be
// empty. (See RFC 7230, Section 5.3)**
//
// For client requests, the URL's Host specifies the server to
// connect to, while the Request's Host field optionally
// specifies the Host header value to send in the HTTP
// request.
URL *url.URL



Then I've understood that why it's returning and empty string for r.URL.Host.



I have also tried r.Header.Get("Host") and then r.Header.Get("Origin"). It also gave me an empty string.



Any other way to get the host name?










share|improve this question


















  • 1





    For // most requests, fields other than Path and RawQuery will be // empty. (See RFC 7230, Section 5.3) this line from godoc describes well that why the r.URL.Host is empty. But is there any other way to get a non empty host name?

    – Anik Hasibul
    Nov 24 '18 at 20:19
















1















I was trying to redirect my client to https url. And I tried this:



func index(w http.ResponseWriter, r *http.Request) {
if r.URL.Scheme != "https" {
http.Redirect(w, r, "https://"+r.URL.Host+r.URL.Path, 301)
return
}
//....
}


But it gave me this response:



$ curl -i http://localhost
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: https:///
Date: Sat, 24 Nov 2018 20:02:33 GMT
Content-Length: 44

<a href="https:///">Moved Permanently</a>.


The mysterious thing is Location: https:/// this line. And I read the go doc again and found:




// URL specifies either the URI being requested (for server
// requests) or the URL to access (for client requests).
//
// For server requests the URL is parsed from the URI
// supplied on the Request-Line as stored in RequestURI. **For
// most requests, fields other than Path and RawQuery will be
// empty. (See RFC 7230, Section 5.3)**
//
// For client requests, the URL's Host specifies the server to
// connect to, while the Request's Host field optionally
// specifies the Host header value to send in the HTTP
// request.
URL *url.URL



Then I've understood that why it's returning and empty string for r.URL.Host.



I have also tried r.Header.Get("Host") and then r.Header.Get("Origin"). It also gave me an empty string.



Any other way to get the host name?










share|improve this question


















  • 1





    For // most requests, fields other than Path and RawQuery will be // empty. (See RFC 7230, Section 5.3) this line from godoc describes well that why the r.URL.Host is empty. But is there any other way to get a non empty host name?

    – Anik Hasibul
    Nov 24 '18 at 20:19














1












1








1








I was trying to redirect my client to https url. And I tried this:



func index(w http.ResponseWriter, r *http.Request) {
if r.URL.Scheme != "https" {
http.Redirect(w, r, "https://"+r.URL.Host+r.URL.Path, 301)
return
}
//....
}


But it gave me this response:



$ curl -i http://localhost
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: https:///
Date: Sat, 24 Nov 2018 20:02:33 GMT
Content-Length: 44

<a href="https:///">Moved Permanently</a>.


The mysterious thing is Location: https:/// this line. And I read the go doc again and found:




// URL specifies either the URI being requested (for server
// requests) or the URL to access (for client requests).
//
// For server requests the URL is parsed from the URI
// supplied on the Request-Line as stored in RequestURI. **For
// most requests, fields other than Path and RawQuery will be
// empty. (See RFC 7230, Section 5.3)**
//
// For client requests, the URL's Host specifies the server to
// connect to, while the Request's Host field optionally
// specifies the Host header value to send in the HTTP
// request.
URL *url.URL



Then I've understood that why it's returning and empty string for r.URL.Host.



I have also tried r.Header.Get("Host") and then r.Header.Get("Origin"). It also gave me an empty string.



Any other way to get the host name?










share|improve this question














I was trying to redirect my client to https url. And I tried this:



func index(w http.ResponseWriter, r *http.Request) {
if r.URL.Scheme != "https" {
http.Redirect(w, r, "https://"+r.URL.Host+r.URL.Path, 301)
return
}
//....
}


But it gave me this response:



$ curl -i http://localhost
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: https:///
Date: Sat, 24 Nov 2018 20:02:33 GMT
Content-Length: 44

<a href="https:///">Moved Permanently</a>.


The mysterious thing is Location: https:/// this line. And I read the go doc again and found:




// URL specifies either the URI being requested (for server
// requests) or the URL to access (for client requests).
//
// For server requests the URL is parsed from the URI
// supplied on the Request-Line as stored in RequestURI. **For
// most requests, fields other than Path and RawQuery will be
// empty. (See RFC 7230, Section 5.3)**
//
// For client requests, the URL's Host specifies the server to
// connect to, while the Request's Host field optionally
// specifies the Host header value to send in the HTTP
// request.
URL *url.URL



Then I've understood that why it's returning and empty string for r.URL.Host.



I have also tried r.Header.Get("Host") and then r.Header.Get("Origin"). It also gave me an empty string.



Any other way to get the host name?







http url go request






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 20:18









Anik HasibulAnik Hasibul

121112




121112








  • 1





    For // most requests, fields other than Path and RawQuery will be // empty. (See RFC 7230, Section 5.3) this line from godoc describes well that why the r.URL.Host is empty. But is there any other way to get a non empty host name?

    – Anik Hasibul
    Nov 24 '18 at 20:19














  • 1





    For // most requests, fields other than Path and RawQuery will be // empty. (See RFC 7230, Section 5.3) this line from godoc describes well that why the r.URL.Host is empty. But is there any other way to get a non empty host name?

    – Anik Hasibul
    Nov 24 '18 at 20:19








1




1





For // most requests, fields other than Path and RawQuery will be // empty. (See RFC 7230, Section 5.3) this line from godoc describes well that why the r.URL.Host is empty. But is there any other way to get a non empty host name?

– Anik Hasibul
Nov 24 '18 at 20:19





For // most requests, fields other than Path and RawQuery will be // empty. (See RFC 7230, Section 5.3) this line from godoc describes well that why the r.URL.Host is empty. But is there any other way to get a non empty host name?

– Anik Hasibul
Nov 24 '18 at 20:19












2 Answers
2






active

oldest

votes


















1














Try using r.Host?



The docs say:



// For server requests Host specifies the host on which the URL
// is sought. Per RFC 7230, section 5.4, this is either the value
// of the "Host" header or the host name given in the URL itself.


So Maybe try that?



func index(w http.ResponseWriter, r *http.Request) {
if r.URL.Scheme != "https" {
http.Redirect(w, r, "https://"+r.Host+r.URL.Path, 301)
return
}
//....
}





share|improve this answer
























  • Thanks it worked, and accepted as answer for answering with example.

    – Anik Hasibul
    Nov 24 '18 at 20:42






  • 1





    For non-proxy requests, r.URL.Scheme is always the empty string. You can reduce the body of the handler to the http.Redirect call.

    – ThunderCat
    Nov 24 '18 at 22:02



















2














From go doc http.request:



type Request struct {
...
// For incoming requests, the Host header is promoted to the
// Request.Host field and removed from the Header map.
...
Header Header
...
// For server requests Host specifies the host on which the
// URL is sought. Per RFC 2616, this is either the value of
// the "Host" header or the host name given in the URL itself.
...
Host string


Thus, use r.Host not r.Header.Get("Host")






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462002%2fnet-http-request-url-host-returns-empty-string%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Try using r.Host?



    The docs say:



    // For server requests Host specifies the host on which the URL
    // is sought. Per RFC 7230, section 5.4, this is either the value
    // of the "Host" header or the host name given in the URL itself.


    So Maybe try that?



    func index(w http.ResponseWriter, r *http.Request) {
    if r.URL.Scheme != "https" {
    http.Redirect(w, r, "https://"+r.Host+r.URL.Path, 301)
    return
    }
    //....
    }





    share|improve this answer
























    • Thanks it worked, and accepted as answer for answering with example.

      – Anik Hasibul
      Nov 24 '18 at 20:42






    • 1





      For non-proxy requests, r.URL.Scheme is always the empty string. You can reduce the body of the handler to the http.Redirect call.

      – ThunderCat
      Nov 24 '18 at 22:02
















    1














    Try using r.Host?



    The docs say:



    // For server requests Host specifies the host on which the URL
    // is sought. Per RFC 7230, section 5.4, this is either the value
    // of the "Host" header or the host name given in the URL itself.


    So Maybe try that?



    func index(w http.ResponseWriter, r *http.Request) {
    if r.URL.Scheme != "https" {
    http.Redirect(w, r, "https://"+r.Host+r.URL.Path, 301)
    return
    }
    //....
    }





    share|improve this answer
























    • Thanks it worked, and accepted as answer for answering with example.

      – Anik Hasibul
      Nov 24 '18 at 20:42






    • 1





      For non-proxy requests, r.URL.Scheme is always the empty string. You can reduce the body of the handler to the http.Redirect call.

      – ThunderCat
      Nov 24 '18 at 22:02














    1












    1








    1







    Try using r.Host?



    The docs say:



    // For server requests Host specifies the host on which the URL
    // is sought. Per RFC 7230, section 5.4, this is either the value
    // of the "Host" header or the host name given in the URL itself.


    So Maybe try that?



    func index(w http.ResponseWriter, r *http.Request) {
    if r.URL.Scheme != "https" {
    http.Redirect(w, r, "https://"+r.Host+r.URL.Path, 301)
    return
    }
    //....
    }





    share|improve this answer













    Try using r.Host?



    The docs say:



    // For server requests Host specifies the host on which the URL
    // is sought. Per RFC 7230, section 5.4, this is either the value
    // of the "Host" header or the host name given in the URL itself.


    So Maybe try that?



    func index(w http.ResponseWriter, r *http.Request) {
    if r.URL.Scheme != "https" {
    http.Redirect(w, r, "https://"+r.Host+r.URL.Path, 301)
    return
    }
    //....
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 24 '18 at 20:36









    jrwrenjrwren

    12.3k52849




    12.3k52849













    • Thanks it worked, and accepted as answer for answering with example.

      – Anik Hasibul
      Nov 24 '18 at 20:42






    • 1





      For non-proxy requests, r.URL.Scheme is always the empty string. You can reduce the body of the handler to the http.Redirect call.

      – ThunderCat
      Nov 24 '18 at 22:02



















    • Thanks it worked, and accepted as answer for answering with example.

      – Anik Hasibul
      Nov 24 '18 at 20:42






    • 1





      For non-proxy requests, r.URL.Scheme is always the empty string. You can reduce the body of the handler to the http.Redirect call.

      – ThunderCat
      Nov 24 '18 at 22:02

















    Thanks it worked, and accepted as answer for answering with example.

    – Anik Hasibul
    Nov 24 '18 at 20:42





    Thanks it worked, and accepted as answer for answering with example.

    – Anik Hasibul
    Nov 24 '18 at 20:42




    1




    1





    For non-proxy requests, r.URL.Scheme is always the empty string. You can reduce the body of the handler to the http.Redirect call.

    – ThunderCat
    Nov 24 '18 at 22:02





    For non-proxy requests, r.URL.Scheme is always the empty string. You can reduce the body of the handler to the http.Redirect call.

    – ThunderCat
    Nov 24 '18 at 22:02













    2














    From go doc http.request:



    type Request struct {
    ...
    // For incoming requests, the Host header is promoted to the
    // Request.Host field and removed from the Header map.
    ...
    Header Header
    ...
    // For server requests Host specifies the host on which the
    // URL is sought. Per RFC 2616, this is either the value of
    // the "Host" header or the host name given in the URL itself.
    ...
    Host string


    Thus, use r.Host not r.Header.Get("Host")






    share|improve this answer




























      2














      From go doc http.request:



      type Request struct {
      ...
      // For incoming requests, the Host header is promoted to the
      // Request.Host field and removed from the Header map.
      ...
      Header Header
      ...
      // For server requests Host specifies the host on which the
      // URL is sought. Per RFC 2616, this is either the value of
      // the "Host" header or the host name given in the URL itself.
      ...
      Host string


      Thus, use r.Host not r.Header.Get("Host")






      share|improve this answer


























        2












        2








        2







        From go doc http.request:



        type Request struct {
        ...
        // For incoming requests, the Host header is promoted to the
        // Request.Host field and removed from the Header map.
        ...
        Header Header
        ...
        // For server requests Host specifies the host on which the
        // URL is sought. Per RFC 2616, this is either the value of
        // the "Host" header or the host name given in the URL itself.
        ...
        Host string


        Thus, use r.Host not r.Header.Get("Host")






        share|improve this answer













        From go doc http.request:



        type Request struct {
        ...
        // For incoming requests, the Host header is promoted to the
        // Request.Host field and removed from the Header map.
        ...
        Header Header
        ...
        // For server requests Host specifies the host on which the
        // URL is sought. Per RFC 2616, this is either the value of
        // the "Host" header or the host name given in the URL itself.
        ...
        Host string


        Thus, use r.Host not r.Header.Get("Host")







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 20:37









        Steffen UllrichSteffen Ullrich

        61.7k358100




        61.7k358100






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462002%2fnet-http-request-url-host-returns-empty-string%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen