本文整理匯總了Golang中http.Request.Header方法的典型用法代碼示例。如果您正苦於以下問題:Golang Request.Header方法的具體用法?Golang Request.Header怎麽用?Golang Request.Header使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類http.Request
的用法示例。
在下文中一共展示了Request.Header方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: authPut
// Put issues a PUT to the specified URL.
//
// Caller should close r.Body when done reading it.
func authPut(url, user, pwd, client, clientURL, version, agent, bodyType string,
body io.Reader) (r *http.Response, err os.Error) {
var req http.Request
req.Method = "PUT"
req.Body = body.(io.ReadCloser)
if user != "" && pwd != "" {
req.Header = map[string][]string{
"Content-Type": {bodyType},
"Transfer-Encoding": {"chunked"},
"User-Agent": {agent},
"X-FluidDB-Client": {client},
"X-FluidDB-Client-URL": {clientURL},
"X-FluidDB-Version": {version},
"Authorization": {"Basic " + encodedUsernameAndPassword(user, pwd)},
}
} else {
req.Header = map[string][]string{
"Content-Type": {bodyType},
"Transfer-Encoding": {"chunked"},
"User-Agent": {agent},
"X-FluidDB-Client": {client},
"X-FluidDB-Client-URL": {clientURL},
"X-FluidDB-Version": {version},
}
}
req.URL, err = http.ParseURL(url)
if err != nil {
return nil, err
}
return send(&req)
}
示例2: TestRequestQueueLess
func TestRequestQueueLess(t *testing.T) {
q := new(requestQueue)
var a, b, c, d http.Request
a.Header = map[string]string{
"X-Pri": "1",
}
b.Header = map[string]string{
"X-Pri": "2",
}
c.Header = map[string]string{
"X-Pri": "1",
}
d.Header = map[string]string{} // default X-Pri: 5000
q.Push(&clientRequest{r: &a})
q.Push(&clientRequest{r: &b})
q.Push(&clientRequest{r: &c})
q.Push(&clientRequest{r: &d})
if !q.Less(0, 1) {
t.Error("want a < b")
}
if q.Less(1, 0) {
t.Error("want b > a")
}
if !q.Less(2, 1) {
t.Error("want c < b")
}
if q.Less(1, 2) {
t.Error("want b > c")
}
if q.Less(0, 2) {
t.Error("want a == c")
}
if q.Less(2, 0) {
t.Error("want c == a")
}
if !q.Less(0, 3) {
t.Error("want a < d")
}
if q.Less(3, 0) {
t.Error("want d > a")
}
}
示例3: post
func post(url_ string, oauthHeaders map[string]string) (r *http.Response, err os.Error) {
var req http.Request
req.Method = "POST"
req.ProtoMajor = 1
req.ProtoMinor = 1
req.Close = true
req.Header = map[string][]string{
"Authorization": {"OAuth "},
}
req.TransferEncoding = []string{"chunked"}
first := true
for k, v := range oauthHeaders {
if first {
first = false
} else {
req.Header["Authorization"][0] += ",\n "
}
req.Header["Authorization"][0] += k + "=\"" + v + "\""
}
req.URL, err = url.Parse(url_)
if err != nil {
return nil, err
}
return send(&req)
}
示例4: NewRequest
func NewRequest(method string, url string, doc IDocument) *http.Request {
var req http.Request
req.Method = method
req.ProtoMajor = 1
req.ProtoMinor = 1
req.Close = true
req.Header = map[string]string{
"Content-Type": "application/json",
"X-Riak-ClientId": "riak.go",
}
if doc.VectorClock() != "" {
req.Header["X-Riak-Vclock"] = doc.VectorClock()
}
req.TransferEncoding = []string{"chunked"}
req.URL, _ = http.ParseURL(url)
if doc.Json() != "" {
cb := &ClosingBuffer{bytes.NewBufferString(doc.Json())}
var rc io.ReadCloser
rc = cb
req.Body = rc
}
fmt.Println(req.URL)
return &req
}
示例5: Get
// Much like http.Get. If s is nil, uses DefaultSender.
func Get(s Sender, url string) (rs []*http.Response, err os.Error) {
for redirect := 0; ; redirect++ {
if redirect >= 10 {
err = os.ErrorString("stopped after 10 redirects")
break
}
var req http.Request
req.RawURL = url
req.Header = map[string]string{}
r, err := Send(s, &req)
if err != nil {
break
}
rs = prepend(r, rs)
if shouldRedirect(r.StatusCode) {
r.Body.Close()
if url = r.GetHeader("Location"); url == "" {
err = os.ErrorString(fmt.Sprintf("%d response missing Location header", r.StatusCode))
break
}
continue
}
return
}
err = &http.URLError{"Get", url, err}
return
}
示例6: Delete
func Delete(url string) *HttpRequestBuilder {
var req http.Request
req.Method = "DELETE"
req.Header = http.Header{}
req.Header.Set("User-Agent", defaultUserAgent)
return &HttpRequestBuilder{url, &req, nil, map[string]string{}}
}
示例7: connect
func (conn *streamConn) connect() (*http.Response, os.Error) {
if conn.stale {
return nil, os.NewError("Stale connection")
}
tcpConn, err := net.Dial("tcp", "", conn.url.Host+":80")
if err != nil {
return nil, err
}
conn.clientConn = http.NewClientConn(tcpConn, nil)
var req http.Request
req.URL = conn.url
req.Method = "GET"
req.Header = map[string]string{}
req.Header["Authorization"] = "Basic " + conn.authData
if conn.postData != "" {
req.Method = "POST"
req.Body = nopCloser{bytes.NewBufferString(conn.postData)}
req.ContentLength = int64(len(conn.postData))
req.Header["Content-Type"] = "application/x-www-form-urlencoded"
}
err = conn.clientConn.Write(&req)
if err != nil {
return nil, err
}
resp, err := conn.clientConn.Read()
if err != nil {
return nil, err
}
return resp, nil
}
示例8: send
// sent a request off to twitter. Returns the response's body or an error.
func send(url, method string, form map[string][]string, client *Client, body string) (result string, err os.Error) {
req := new(http.Request)
req.Method = method
req.RawURL = url
req.Host = URLHost
req.Referer = "none"
req.UserAgent = HTTPUserAgent
req.Form = form
req.Header = map[string]string{
"Connection": "Keep Alive",
"Authorization": getAuthHeader(client),
}
req.Body = strings.NewReader(body)
req.URL, err = http.ParseURL(req.RawURL)
if err != nil {
return "", err
}
// send request
resp := new(http.Response)
resp, err = http.Send(req)
if err != nil {
return "", err
}
result = getResponseBody(resp)
return result, nil
}
示例9: post
func post(theUrl string, oauthHeaders map[string]string) (r *http.Response, err os.Error) {
var req http.Request
var authorization string = "OAuth "
req.Method = "POST"
req.ProtoMajor = 1
req.ProtoMinor = 1
req.Close = true
req.Header = http.Header{}
req.TransferEncoding = []string{"chunked"}
first := true
for k, v := range oauthHeaders {
if first {
first = false
} else {
authorization += ",\n "
}
authorization += k + "=\"" + v + "\""
}
req.Header.Add("Authorization", authorization)
req.URL, err = url.Parse(theUrl)
if err != nil {
return nil, err
}
return send(&req)
}
示例10: Get
// Perform a GET request for the test t.
func Get(t *Test) (r *http.Response, finalUrl string, cookies []*http.Cookie, err os.Error) {
var url = t.Url // <-- Patched
var req http.Request
req.Method = "GET"
req.ProtoMajor = 1
req.ProtoMinor = 1
req.Header = http.Header{}
if len(t.Param) > 0 {
ep := http.EncodeQuery(t.Param)
if strings.Contains(url, "?") {
url = url + "&" + ep
} else {
url = url + "?" + ep
}
}
req.URL, err = http.ParseURL(url)
if err != nil {
err = &http.URLError{"Get", url, err}
return
}
addHeadersAndCookies(&req, t)
url = req.URL.String()
debug("Will get from %s", req.URL.String())
r, finalUrl, cookies, err = DoAndFollow(&req, t.Dump)
return
}
示例11: httpExecute
func (c *Consumer) httpExecute(
method string, url string, body string, oauthParams *OrderedParams) (*http.Response, os.Error) {
if c.debug {
fmt.Println("httpExecute(method: " + method + ", url: " + url)
}
var req http.Request
req.Method = method
req.Header = http.Header{}
req.Body = newStringReadCloser(body)
parsedurl, err := http.ParseURL(url)
if err != nil {
return nil, os.NewError("ParseUrl: " + err.String())
}
req.URL = parsedurl
oauthHdr := "OAuth "
for pos, key := range oauthParams.Keys() {
if pos > 0 {
oauthHdr += ","
}
oauthHdr += key + "=\"" + oauthParams.Get(key) + "\""
}
if c.debug {
fmt.Println("AUTH-HDR: " + oauthHdr)
}
req.Header.Add("Authorization", oauthHdr)
resp, err := c.HttpClient.Do(&req)
if err != nil {
return nil, os.NewError("Do: " + err.String())
}
debugHeader := ""
for k, vals := range req.Header {
for _, val := range vals {
debugHeader += "[key: " + k + ", val: " + val + "]"
}
}
if resp.StatusCode != http.StatusOK {
bytes, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
return nil, os.NewError("HTTP response is not 200/OK as expected. Actual response: \n" +
"\tResponse Status: '" + resp.Status + "'\n" +
"\tResponse Code: " + strconv.Itoa(resp.StatusCode) + "\n" +
"\tResponse Body: " + string(bytes) + "\n" +
"\tRequst Headers: " + debugHeader)
}
return resp, err
}
示例12: sendBody
func sendBody(s Sender, url, method, bodyType string, body io.Reader) (r *http.Response, err os.Error) {
var req http.Request
req.Method = method
req.RawURL = url
req.Body = nopCloser{body}
req.Header = map[string]string{
"Content-Type": bodyType,
}
req.TransferEncoding = []string{"chunked"}
return Send(s, &req)
}
示例13: Send
func Send(s Sender, req *http.Request) (resp *http.Response, err os.Error) {
if s == nil {
s = DefaultSender
}
req.ProtoMajor = 1
req.ProtoMinor = 1
header := req.Header
req.Header = map[string]string{}
for k, v := range header {
req.Header[http.CanonicalHeaderKey(k)] = v
}
return s.Send(req)
}
示例14: Get
func Get(url, user, pwd string) (r *http.Response, err os.Error) {
var req http.Request
req.Header = map[string]string{"Authorization": "Basic " +
encodedUsernameAndPassword(user, pwd)}
if req.URL, err = http.ParseURL(url); err != nil {
return
}
if r, err = send(&req); err != nil {
return
}
return
}
示例15: authGet
func authGet(url, user, pwd string) (r *http.Response, err os.Error) {
var req http.Request
h := make(http.Header)
h.Add("Authorization", "Basic "+
encodedUsernameAndPassword(user, pwd))
req.Header = h
if req.URL, err = http.ParseURL(url); err != nil {
return
}
if r, err = send(&req); err != nil {
return
}
return
}