本文整理匯總了Golang中http.Request.AddCookie方法的典型用法代碼示例。如果您正苦於以下問題:Golang Request.AddCookie方法的具體用法?Golang Request.AddCookie怎麽用?Golang Request.AddCookie使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類http.Request
的用法示例。
在下文中一共展示了Request.AddCookie方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: buildTestRequest
func buildTestRequest(method string, path string, body string, headers map[string][]string, cookies []*http.Cookie) *http.Request {
host := "127.0.0.1"
port := "80"
rawurl := "http://" + host + ":" + port + path
url_, _ := url.Parse(rawurl)
proto := "HTTP/1.1"
if headers == nil {
headers = map[string][]string{}
}
headers["User-Agent"] = []string{"web.go test"}
if method == "POST" {
headers["Content-Length"] = []string{fmt.Sprintf("%d", len(body))}
if headers["Content-Type"] == nil {
headers["Content-Type"] = []string{"text/plain"}
}
}
req := http.Request{Method: method,
RawURL: rawurl,
URL: url_,
Proto: proto,
Host: host,
Header: http.Header(headers),
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
}
for _, cookie := range cookies {
req.AddCookie(cookie)
}
return &req
}
示例2: parseCookieHeader
func parseCookieHeader(cookieHeader string, request *http.Request) {
// break up into individual cookies
cookieStrings := strings.Split(cookieHeader, ";")
for _, cookieString := range cookieStrings {
var cookie *http.Cookie = new(http.Cookie)
cookieStringSegments := strings.Split(strings.TrimSpace(cookieString), "=")
cookie.Name = cookieStringSegments[0]
cookie.Value = cookieStringSegments[1]
request.AddCookie(cookie)
}
}