mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
net/url: add example of using URL.Opaque with http.Request
Per private thread soliciting help. I realized part of this is documented in several places, but we lacked a unifying example. LGTM=rsc R=golang-codereviews CC=adg, golang-codereviews, iant, rsc https://golang.org/cl/171620043
This commit is contained in:
parent
1a60ea1c01
commit
38ea0ae05f
1 changed files with 30 additions and 0 deletions
|
|
@ -7,7 +7,10 @@ package url_test
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExampleValues() {
|
||||
|
|
@ -39,3 +42,30 @@ func ExampleURL() {
|
|||
fmt.Println(u)
|
||||
// Output: https://google.com/search?q=golang
|
||||
}
|
||||
|
||||
func ExampleURL_opaque() {
|
||||
// Sending a literal '%' in an HTTP request's Path
|
||||
req := &http.Request{
|
||||
Method: "GET",
|
||||
Host: "example.com", // takes precendence over URL.Host
|
||||
URL: &url.URL{
|
||||
Host: "ignored",
|
||||
Scheme: "https",
|
||||
Opaque: "/%2f/",
|
||||
},
|
||||
Header: http.Header{
|
||||
"User-Agent": {"godoc-example/0.1"},
|
||||
},
|
||||
}
|
||||
out, err := httputil.DumpRequestOut(req, true)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(strings.Replace(string(out), "\r", "", -1))
|
||||
// Output:
|
||||
// GET /%2f/ HTTP/1.1
|
||||
// Host: example.com
|
||||
// User-Agent: godoc-example/0.1
|
||||
// Accept-Encoding: gzip
|
||||
//
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue