Golang HTTP GET Request Returning 404

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.

+4
source share
2 answers

The problem is unicode related. My team (not showing up in my original question) had %2F , that Go was converted to / , which was supposed to stay as %2F (cURL correctly left it as %2F ). Changing %2F to %252F resolves the issue.

It also seems that when creating a new HTTP request, Go will parse your Unicode back into plain text, so if you have %3D in the URL sent to the HTTP request initializer, it will convert it to = . I thought the obvious solution would be to put %253D in the URL, but apparently there is an error in Go that converts %3D to = , but NOT %25 to % . I had to use an opaque URL request ( request.Url.Opaque ) to get around this.

+2
source

You say that the curl command works, but if you have the last twist (I have 7.22.0). The request would be something like this:

curl url --verbose

GET / HTTP / 1.1

User-Agent: curl / 7.22.0 (x86_64-pc-linux-gnu) libcurl / 7.22.0 OpenSSL / 1.0.1 zlib / 1.2.3.4 libidn / 1.23 librtmp / 2.3

Host: myhost.com

To accept:/

But you set req.Proto = "HTTP/1.0" . It seems not true.

0
source

All Articles