HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoMajor

Basic HTTP Auth in Go

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
basichttpauth

Problem

I'm trying to do basic HTTP auth with the code below, but it is throwing out the following error:

2013/05/21 10:22:58 Get mydomain.example: unsupported protocol scheme ""
exit status 1

func basicAuth() string {
    var username string = "foo"
    var passwd string = "bar"
    client := &http.Client{}
    req, err := http.NewRequest("GET", "mydomain.example", nil)
    req.SetBasicAuth(username, passwd)
    resp, err := client.Do(req)
    if err != nil{
        log.Fatal(err)
    }
    bodyText, err := ioutil.ReadAll(resp.Body)
    s := string(bodyText)
    return s
}


Any idea what I may be doing wrong?

Solution

You need to specify the protocol for NewRequest, e.g. "http://", see here.

req, err := http.NewRequest("GET", "http://mydomain.example", nil)

Code Snippets

req, err := http.NewRequest("GET", "http://mydomain.example", nil)

Context

Stack Overflow Q#16673766, score: 71

Revisions (0)

No revisions yet.