Have no luck with extracting fragment data (foo
in http://domain.com/path#foo) with standard http.Server
.
package main
import (
"fmt"
"net/http"
)
type Handler struct {
}
func (handler Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Path = \"%v\" Fragment = \"%v\"
", r.URL.Path, r.URL.Fragment)
}
func main() {
var handler Handler
http.ListenAndServe(":30000", handler)
}
produces empty fragment for http://127.0.0.1:30000/path#foo
:
Path = "/path" Fragment = ""
How can I get fragment data using golang's builtin http.Server
?
You can't. This isn't a Go thing -- URL fragments are not sent to the server over HTTP. They're just a browser concept.
Here is a relevant issue, where the docs for http.Request
were changed to say:
// For server requests the URL is parsed from the URI
// supplied on the Request-Line as stored in RequestURI. For
// most requests, fields other than Path and RawQuery will be
// empty. (See RFC 2616, Section 5.1.2)
If for some reason you need it, you could probably string together some JavaScript to include the fragment in the request as a GET
parameter or something.