예제 #1
0
파일: cstore_test.go 프로젝트: emk/cstore
// 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
0
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
0
파일: neo4j.go 프로젝트: searchify/Neo4j-GO
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
0
// 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
}
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
0
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
0
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
0
파일: godns.go 프로젝트: manveru/godns
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
0
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
0
파일: notifo.go 프로젝트: chrisfarms/notifo
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
}