本文整理匯總了Golang中http.Client.Do方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.Do方法的具體用法?Golang Client.Do怎麽用?Golang Client.Do使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類http.Client
的用法示例。
在下文中一共展示了Client.Do方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: put
// Send an HTTP PUT request. The caller is responsible for calling
// resp.Body.Close().
func put(client *http.Client, url, data string) (resp *http.Response, err os.Error) {
req, err := http.NewRequest("PUT", url, strings.NewReader(data))
if err != nil {
return
}
resp, err = client.Do(req)
return
}
示例2: PostActivity
func PostActivity(activity *Activity, bearerToken string, client *http.Client) {
outputJSON, _ := json.Marshal(activity)
request, _ := http.NewRequest("POST", "https://api.runkeeper.com/fitnessActivities", strings.NewReader(string(outputJSON)))
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", bearerToken))
request.Header.Add("Content-Type", "application/vnd.com.runkeeper.NewFitnessActivity+json")
response, _ := client.Do(request)
if response.StatusCode == 201 {
fmt.Print("Activity POSTed")
} else {
fmt.Printf("Activity post failed with status code %d and message %s", response.StatusCode, response.Body)
}
}
示例3: send
func (this *Neo4j) send(url string, data string) (string, os.Error) {
var (
resp *http.Response // http response
buf bytes.Buffer // contains http response body
err os.Error
)
if len(url) < 1 {
url = this.URL + "node" // default path
}
client := new(http.Client)
switch strings.ToLower(this.Method) { // which http method
case "delete":
req, e := http.NewRequest("DELETE", url, nil)
if e != nil {
err = e
break
}
resp, err = client.Do(req)
case "post":
body := strings.NewReader(data)
resp, err = client.Post(url,
"application/json",
body,
)
case "put":
body := strings.NewReader(data)
req, e := http.NewRequest("PUT", url, body)
if e != nil {
err = e
break
}
req.Header.Set("Content-Type", "application/json")
resp, err = client.Do(req)
case "get":
fallthrough
default:
resp, err = client.Get(url)
}
if err != nil {
return "", err
}
defer func() {
if resp.Body != nil {
resp.Body.Close()
}
}()
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return "", err
}
this.StatusCode = resp.StatusCode // the calling method should do more inspection with chkStatusCode() method and determine if the operation was successful or not.
return buf.String(), nil
}
示例4: send
// Sign and send a Request using the current configuration.
func (c *UserConfig) send(request *http.Request, service *Service, client *http.Client) (*http.Response, os.Error) {
if err := service.Sign(request, c); err != nil {
return nil, err
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, os.NewError("Endpoint response: " + response.Status)
}
return response, nil
}
示例5: TestIntegration
func TestIntegration(t *testing.T) {
service, userConfig := GetTwitterConfig(t)
httpClient := new(http.Client)
url := "https://api.twitter.com/1/account/verify_credentials.json"
httpRequest, _ := http.NewRequest("GET", url, nil)
service.Sign(httpRequest, userConfig)
httpResponse, err := httpClient.Do(httpRequest)
if err != nil {
t.Error("Response had an error")
}
if httpResponse.StatusCode != 200 {
t.Errorf("Response returned code of %v", httpResponse.StatusCode)
}
}
示例6: YadisRequest
func YadisRequest(url_ string, method string) (resp *http.Response, err os.Error) {
resp = nil
var request = new(http.Request)
var client = new(http.Client)
var Header = make(http.Header)
request.Method = method
request.RawURL = url_
request.URL, err = url.Parse(url_)
if err != nil {
return
}
// Common parameters
request.Proto = "HTTP/1.0"
request.ProtoMajor = 1
request.ProtoMinor = 0
request.ContentLength = 0
request.Close = true
Header.Add("Accept", "application/xrds+xml")
request.Header = Header
// Follow a maximum of 5 redirections
for i := 0; i < 5; i++ {
response, err := client.Do(request)
if err != nil {
return nil, err
}
if response.StatusCode == 301 || response.StatusCode == 302 || response.StatusCode == 303 || response.StatusCode == 307 {
location := response.Header.Get("Location")
request.RawURL = location
request.URL, err = url.Parse(location)
if err != nil {
return
}
} else {
return response, nil
}
}
return nil, os.NewError("Too many redirections")
}
示例7: NewStream
func NewStream(username, password string) (*RawStream, os.Error) {
var s = &RawStream{Updates: make(chan Update, 100)}
client := new(http.Client)
req, _ := http.NewRequest("GET", FEED_URL, nil)
req.SetBasicAuth(username, password)
if res, err := client.Do(req); err == nil {
if res.StatusCode == 200 {
s.body = res.Body
go s.process()
} else {
return nil, os.NewError(res.Status)
}
} else {
return nil, err
}
return s, nil
}
示例8: nmcPOST
func nmcPOST(body *bytes.Buffer) (response *http.Response, err os.Error) {
client := http.Client{}
var req http.Request
req.Method = "POST"
req.ProtoMajor = 1
req.ProtoMinor = 1
req.Close = true
req.Body = ioutil.NopCloser(body)
req.Header = http.Header{
"Content-Type": []string{"text/plain"},
"Content-Length": []string{strconv.Itoa(body.Len())},
}
req.ContentLength = int64(body.Len())
req.URL = options.nmcURL
return client.Do(&req)
}
示例9: handler
func handler(w http.ResponseWriter, req *http.Request) {
out := io.Writer(w)
_ = time.LocalTime()
_ = strings.HasPrefix("a", "b")
if strings.HasSuffix(req.URL.Path, "stream.php") {
log.Print("Found stream.php")
filename := "stream-" + time.LocalTime().String() + ".mp3"
f, e := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
if e != nil {
log.Print("Could not open: ", filename)
return
}
defer f.Close()
out = &TeeWriter{
w,
f,
}
}
c := http.Client{}
resp, e := c.Do(req)
if e != nil {
log.Print("Could not get site: ", e)
return
}
defer resp.Body.Close()
for key, vals := range resp.Header {
for _, val := range vals {
w.Header().Add(key, val)
}
}
_, e = io.Copy(out, resp.Body)
if e != nil {
log.Print("Error while sending data: ", e)
return
}
}
示例10: request
func (nc *Client) request(path string, data url.Values) (*response, os.Error) {
// endpoint
url := "https://api.notifo.com/v1" + path
// encode request params
reqBody := strings.NewReader(data.Encode())
// build request
req, err := http.NewRequest("POST", url, reqBody)
if err != nil {
return nil, err
}
req.Method = "POST"
req.SetBasicAuth(nc.Username, nc.ApiSecret)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// make request
client := new(http.Client)
r, err := client.Do(req)
// check connection
if err != nil {
return nil, err
}
defer r.Body.Close()
// read response
body, _ := ioutil.ReadAll(r.Body)
// decode json
response := new(response)
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
// check for success code
if response.Code != 2201 {
return nil, fmt.Errorf("notifo: %s", response.Message)
}
// no error
return response, nil
}