I am trying to do simple HTTP access using BasicAuth. The problem is that the answer keeps returning "404", although I can copy and paste the URL into the command line cURL request and it works fine:
const url string = "http://1.2.3.4:6710/REST/command" const username string = "..." const password string = "..." fmt.Printf("\n%v\n", url) client := &http.Client{} req, _ := http.NewRequest("GET", url, nil) req.SetBasicAuth(username, password) req.Proto = "HTTP/1.0" req.ProtoMinor = 0 resp, _ := client.Do(req) fmt.Printf("\n%v\n", resp) defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Printf("\n%v\n\n", string(body))
So, you can see that I am printing my url right away - this is the same line of text that if I copy the command line cURL in the request, everything works fine.
Response to my request
&{404 Not Found 404 HTTP/1.0 1 0 map[Pragma:[no-cache] Date:[Wed, 17 Apr 2013 15:01:33 GMT] Connection:[close] Server:[MoneyWorks_Datacentre/6.1.3 Win-x86 REST/6.1.3] Cache-Control:[no-store, no-cache, must-revalidate] Expires:[Wed, 17 Apr 2013 15:01:33 GMT]] 0xf8400e3920 -1 [] true map[] 0xf8400aa000}
Is there something unique to golang HTTP functions that are different from how cURL would handle such a simple request?
EDIT . I work by passing the URL exec.Command("curl", url).Output() . Obviously, this is not a native solution, which I hope for, but it works so far.
source share