Hi Gophers,
I am learning go by writing a monitor application for the web application I support. Our stack runs on JBoss and we have an endpoint called "status".
In every other language I use I can just send a request to https://ourapp.com/status
with the appropriate header set and all is well.
What is confusing me is that in go I have to send the request to https://ourapp.com/status/
note the trailing slash.
Is this something specific to go
that I just need to be aware of or am I seeing some oddity in JBoss/our application specific to how go
handles requests?
package main
import (
"bytes"
"fmt"
"net/http"
)
func main() {
client := &http.Client{}
//Works fine
req, err := http.NewRequest("GET", "https://ourapp.com/status/", nil)
//resp.Body is empty, resp.ContentLength is 0
//req, err := http.NewRequest("GET", "https://ourapp.com/status", nil)
if err != nil {
fmt.Println(err)
}
req.Header.Set("statuskey", "auth_key_redacted")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
fmt.Println("Response")
fmt.Println(resp.Status)
fmt.Println(resp.ContentLength)
fmt.Println(resp.Header)
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
fmt.Println(buf.String())
}
**评论:**
gohacker:
hipone:req.Header.Set("statuskey", "auth_key_redacted")
Probably issue 4800: https://github.com/golang/go/issues/4800
Try Go 1.8 beta 2.
xandout:(net/http).Client with default transport is not doing any magic behind the scenes with your request - if your url is "https://ourapp.com/status/" then path part of it is "/status/" - accordingly if you used "https://ourapp.com/status////" then path would be "/status////".
If servers expects path to be "/status/" then your Java's http client was adding trailing slash by default. The easiest way to know would be to use some debugging proxy to record http communication and confirm or reject this explanation.
ashwch:So based on that it seems it isn't
Client
but the server.Maybe my Chrome/python stuff is modifying the request.
xandout:You can debug this easily in Python using
requests
.requests
preserves headers and also sets appropriates cookies based on the headers returned by the previous request, but Go won't do any of that.>>> import requests >>> r = requests.get('https://ourapp.com/status', headers={<your headers>}) >>> print r.url # print final URL >>> print r.request.headers >>> for h in r.history: print h.request.headers
If some sort of redirection is happening(based on the Location header returned by the above URL) then the above program will print
https://ourapp.com/status/
. You can also check the complete redirection history usingr.history
.In Go you can do something similar by defining your own
CheckRedirect
function and printing the headers being sent with each redirect.
Thanks for that tip, there is a
302
being sent forourapp.com/status/
. Based on my understanding of the docs,net/http
will follow up to 10 redirects but it does not appear to be following this one.Do I need to explicitly state that I would like to follow the 302?
