Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang : Unsolicited response received on idle HTTP channel starting with

Tags:

go

this is my actual code :

func cURL(cURL string, follow bool) (*string, error) {
    var err error
    var resp *http.Response
    var req *http.Request

    var u *url.URL
    if u, err = url.Parse(cURL); nil != err {
        if logLevel >= 30 {
            log.Print("ERROR: ", err)
        }
        return nil, err
    }

    if req, err = http.NewRequest("GET", u.Scheme+"://"+u.Host+u.Path, nil); err != nil {
        if logLevel >= 40 {
            log.Print("ERROR: ", err)
        }
        return nil, err
    }
    req.URL.RawQuery = u.RawQuery

    client := &http.Client{
        Timeout: 3 * time.Second,  //timeout connexion
    }
    if resp, err = client.Do(req); nil != err {
        if logLevel >= 50 {
            log.Print("ERROR: ", err)
        }
        return nil, err
    }
    defer resp.Body.Close()

    var body []byte
    if body, err = ioutil.ReadAll(resp.Body); nil != err {
        if logLevel >= 30 {
            log.Print("ERROR: ", err)
        }
        return nil, err
    }

    var html string
    html = string(body)

    return &html, nil
}

same time print this error in terminal, i want to hide this :

2017/09/29 18:19:28 Unsolicited response received on idle HTTP channel starting with "HTTP/1.0 408 Request Time-out\r\nServer: AkamaiGHost\r\nMime-Version: 1.0\r\nDate: Fri, 29 Sep 2017 16:18:21 GMT\r\nContent-Type: text/html\r\nContent-Length: 218\r\nExpires: Fri, 29 Sep 2017 16:18:21 GMT\r\n\r\n<HTML><HEAD>\n<TITLE>Request Timeout</TITLE>\n</HEAD><BODY>\n<H1>Request Timeout</H1>\nThe server timed out while waiting for the browser's request.<P>\nReference&#32;&#3

i want to hide this error, not print in terminal, how i can do this ? Where im wrong ? i use this function for check large list urls if have specific keyword, the list is really big 50 milions urls.

like image 789
Stefano Conte Avatar asked Sep 29 '17 16:09

Stefano Conte


1 Answers

That message comes from http.Transport within the http.Client, specifically here

log.Printf("Unsolicited response received on idle HTTP channel starting with %q; err=%v", buf, peekErr)

Unfortunately, unlike http.Server which has an ErrorLog field for overriding the default logger, http.Transport always writes to the standard logger.

To prevent the message from being printed you can use log.SetOutput(io.Discard) or log.SetOutput(ioutil.Discard) before 1.16. Keep in mind this will discard everything written to the standard logger. As JimB mentions, if you still want to log in other places, you can create a custom logger.

like image 126
esote Avatar answered Nov 01 '22 05:11

esote