func authenticateWithBackend(req *http.Request) (bool, error) {
	auth, authExists := req.Header["Authorization"]
	if authExists && isCached(auth[0]) {
		return true, nil
	}

	var r *http.Request
	var err error
	var resp *http.Response

	r, err = http.NewRequest("GET", planio_url, nil)
	if err != nil {
		return false, err
	}
	r.Header["Authorization"] = auth

	client := http.Client{}
	resp, err = client.Do(r)
	if err != nil {
		return false, err
	}

	resp.Body.Close()
	if resp.StatusCode == 200 {
		if authExists {
			addToCache(auth[0])
		}
		return true, nil
	}
	return false, nil
}
Esempio n. 2
0
File: http.go Progetto: wcgh/commons
// 通过HTTP上传文件
// 模拟<form ...><input name="file" type="file" />...</form>
// 参数说明
// url: 上传服务器URL
// form_name: 对应<input>标签中的name
// file_name: 为form表单中的文件名
// path: 为实际要上传的本地文件路径
func PostFile(url, form_name, file_name, path string) (res *http.Response, err error) {
	buf := new(bytes.Buffer) // caveat IMO dont use this for large files, \
	// create a tmpfile and assemble your multipart from there (not tested)
	w := multipart.NewWriter(buf)

	fw, err := w.CreateFormFile(form_name, file_name) //这里的file必须和服务器端的FormFile一致
	if err != nil {
		return
	}
	fd, err := os.Open(path)
	if err != nil {
		return
	}
	defer fd.Close()
	// Write file field from file to upload
	_, err = io.Copy(fw, fd)
	if err != nil {
		return
	}
	// Important if you do not close the multipart writer you will not have a
	// terminating boundry
	w.Close()
	req, err := http.NewRequest("POST", url, buf)
	if err != nil {
		return
	}
	req.Header.Set("Content-Type", w.FormDataContentType())
	var client http.Client
	res, err = client.Do(req)
	return
}
Esempio n. 3
0
func TestWatchHTTPAccept(t *testing.T) {
	simpleStorage := &SimpleRESTStorage{}
	handler := handle(map[string]rest.Storage{"simples": simpleStorage})
	server := httptest.NewServer(handler)
	defer server.Close()
	client := http.Client{}

	dest, _ := url.Parse(server.URL)
	dest.Path = "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/simples"
	dest.RawQuery = ""

	request, err := http.NewRequest("GET", dest.String(), nil)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	request.Header.Set("Accept", "application/XYZ")
	response, err := client.Do(request)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	// TODO: once this is fixed, this test will change
	if response.StatusCode != http.StatusNotAcceptable {
		t.Errorf("Unexpected response %#v", response)
	}
}
Esempio n. 4
0
func getUserInfo(target_name string) UserInfo {
	bearer := getToken()
	url := "https://api.twitter.com/1.1/users/show.json?screen_name=" + target_name
	request, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Fatal(err)
	}
	request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", bearer))

	client := new(http.Client)
	response, err := client.Do(request)
	if err != nil {
		log.Fatal(err)
	}
	defer response.Body.Close()

	body, err := ioutil.ReadAll(response.Body)
	util.Perror(err)

	var user_info UserInfo
	parse_err := json.Unmarshal(body, &user_info)
	util.Perror(parse_err)

	return user_info
}
Esempio n. 5
0
func normalGet(method string, c *http.Client, url *url.URL) {
	if c == nil {
		c = http.DefaultClient
	}

	if *verbose {
		log.Printf("%s %s", method, url)
	}

	req, err := http.NewRequest(method, url.String(), nil)
	if err != nil {
		log.Fatal(err)
	}
	resp, err := c.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	if !(resp.StatusCode >= 200 && resp.StatusCode <= 399) {
		log.Fatalf("Error: HTTP %d: %s.", resp.StatusCode, body)
	}

	fmt.Println(string(body))
}
Esempio n. 6
0
func (g *Adaptor) ExpireTokens(userID int) error {
	request := ExpireTokenRequest{Action: "all"}

	gandalfUrl := fmt.Sprintf("http://%s:%d/expire/%d", g.GandalfHost, g.GandalfPort, userID)
	client := http.Client{}

	marshalledReq, err := json.Marshal(request)
	if err != nil {
		return err
	}

	req, err := http.NewRequest("POST", gandalfUrl, bytes.NewBuffer(marshalledReq))
	if err != nil {
		return err
	}

	resp, err := client.Do(req)
	if err != nil {
		return err
	}

	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return errors.New("failed to expire tokens")
	}

	return nil
}
Esempio n. 7
0
// Do sends an HTTP request with the provided http.Client and returns an HTTP response.
// If the client is nil, http.DefaultClient is used.
// If the context is canceled or times out, ctx.Err() will be returned.
func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
	if client == nil {
		client = http.DefaultClient
	}

	// Request cancelation changed in Go 1.5, see cancelreq.go and cancelreq_go14.go.
	cancel := canceler(client, req)

	type responseAndError struct {
		resp *http.Response
		err  error
	}
	result := make(chan responseAndError, 1)

	go func() {
		resp, err := client.Do(req)
		result <- responseAndError{resp, err}
	}()

	select {
	case <-ctx.Done():
		cancel()
		return nil, ctx.Err()
	case r := <-result:
		return r.resp, r.err
	}
}
Esempio n. 8
0
func main() {
	// Create a new request
	req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/account/who_am_i", domain), strings.NewReader(""))
	if err != nil {
		log.Fatalln(err.Error())
	}

	// Set authentication and add Accept header
	req.SetBasicAuth(username, password)
	req.Header.Add("Accept", "application/json")

	// Perform the request
	client := http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Fatalln(err.Error())
	}

	// Read the response body
	body, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		log.Fatalln(err.Error())
	}

	log.Println(string(body))
}
Esempio n. 9
0
func getBlockIndexesForServer(t *testing.T, i int) []string {
	var indexes []string

	path := keepServers[i] + "/index"
	client := http.Client{}
	req, err := http.NewRequest("GET", path, nil)
	req.Header.Add("Authorization", "OAuth2 "+arvadostest.DataManagerToken)
	req.Header.Add("Content-Type", "application/octet-stream")
	resp, err := client.Do(req)
	defer resp.Body.Close()

	if err != nil {
		t.Fatalf("Error during %s %s", path, err)
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatalf("Error reading response from %s %s", path, err)
	}

	lines := strings.Split(string(body), "\n")
	for _, line := range lines {
		indexes = append(indexes, strings.Split(line, " ")...)
	}

	return indexes
}
Esempio n. 10
0
// get take an http.Client pointer and URL and sends a GET request to the
// server.
func (c *Client) get(httpClient *http.Client, url string) (int, []byte, error) {
	c.debugln("get(): Creating new request")
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return 0, nil, err
	}

	if c.token != "" {
		req.Header.Set("X-Cookie", "token="+c.token)
	}

	if c.accessKey != "" && c.secretKey != "" {
		header := fmt.Sprintf("accessKey=%s; secretKey=%s;", c.accessKey, c.secretKey)
		req.Header.Set("X-ApiKeys", header)
	}

	c.debugln("get(): Executing request")
	resp, err := httpClient.Do(req)
	if err != nil {
		return resp.StatusCode, nil, err
	}
	defer resp.Body.Close()

	c.debugln("get(): Reading body of response")
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return resp.StatusCode, nil, err
	}

	return resp.StatusCode, body, err
}
Esempio n. 11
0
// postWithArgs takes an http.Client pointer, URL, and JSON byte array,
// sends a POST request to the server, and then returns the response body as a
// byte array with a nil error. Otherwise, the array will be nil and an error
// will be passed
func (c *Client) postWithJSON(httpClient *http.Client, url string, jsonStr []byte) (int, []byte, error) {
	c.debugln("postWithJSON(): Creating new request")
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
	if err != nil {
		return 0, nil, err
	}
	req.Header.Set("Content-Type", "application/json")

	if c.token != "" {
		req.Header.Set("X-Cookie", "token="+c.token)
	}

	if c.accessKey != "" && c.secretKey != "" {
		header := fmt.Sprintf("accessKey=%s; secretKey=%s;", c.accessKey, c.secretKey)
		req.Header.Set("X-ApiKeys", header)
	}

	c.debugln("postWithJSON(): Executing request")
	resp, err := httpClient.Do(req)
	if err != nil {
		return 0, nil, err
	}
	defer resp.Body.Close()

	c.debugln("postWithJSON(): Reading body of response")
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return 0, nil, err
	}

	return resp.StatusCode, body, nil
}
Esempio n. 12
0
//HTTPComplexRequest allows more control over request options
func HTTPComplexRequest(c *HTTPRequestConfig) (*HTTPResponse, error) {
	hresp := &HTTPResponse{}
	req, err := http.NewRequest(c.Method, c.URL, c.Body)
	if err != nil {
		return hresp, err
	}
	for k, v := range c.Headers {
		req.Header.Add(k, v)
	}
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: c.InsecureSkipTLSVerify},
	}
	hc := http.Client{
		Transport: tr,
		Timeout:   time.Duration(c.TimeoutSeconds) * time.Second,
	}
	resp, err := hc.Do(req)
	if err != nil {
		return hresp, err
	}
	bs, bb, err := getRespBody(resp)
	if err != nil {
		return hresp, err
	}
	hresp.Body = bs
	hresp.BodyBytes = bb
	hresp.Resp = resp
	if resp.StatusCode > 399 && c.FailOnError {
		return hresp, fmt.Errorf("Server response indicates failure: %v %v", resp.StatusCode, bs)
	}
	return hresp, nil
}
Esempio n. 13
0
File: github.go Progetto: jwmaag/dex
// user queries the GitHub API for profile information using the provided client. The HTTP
// client is expected to be constructed by the golang.org/x/oauth2 package, which inserts
// a bearer token as part of the request.
func (c *githubConnector) user(ctx context.Context, client *http.Client) (user, error) {
	var u user
	req, err := http.NewRequest("GET", baseURL+"/user", nil)
	if err != nil {
		return u, fmt.Errorf("github: new req: %v", err)
	}
	req = req.WithContext(ctx)
	resp, err := client.Do(req)
	if err != nil {
		return u, fmt.Errorf("github: get URL %v", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return u, fmt.Errorf("github: read body: %v", err)
		}
		return u, fmt.Errorf("%s: %s", resp.Status, body)
	}

	if err := json.NewDecoder(resp.Body).Decode(&u); err != nil {
		return u, fmt.Errorf("failed to decode response: %v", err)
	}
	return u, nil
}
Esempio n. 14
0
func TestWatchHTTP(t *testing.T) {
	simpleStorage := &SimpleRESTStorage{}
	handler := handle(map[string]rest.Storage{"simples": simpleStorage})
	server := httptest.NewServer(handler)
	defer server.Close()
	client := http.Client{}

	dest, _ := url.Parse(server.URL)
	dest.Path = "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/watch/simples"
	dest.RawQuery = ""

	request, err := http.NewRequest("GET", dest.String(), nil)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	response, err := client.Do(request)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if response.StatusCode != http.StatusOK {
		t.Errorf("Unexpected response %#v", response)
	}

	decoder := json.NewDecoder(response.Body)

	for i, item := range watchTestTable {
		// Send
		simpleStorage.fakeWatch.Action(item.t, item.obj)
		// Test receive
		var got watchJSON
		err := decoder.Decode(&got)
		if err != nil {
			t.Fatalf("%d: Unexpected error: %v", i, err)
		}
		if got.Type != item.t {
			t.Errorf("%d: Unexpected type: %v", i, got.Type)
		}
		t.Logf("obj: %v", string(got.Object))
		gotObj, err := codec.Decode(got.Object)
		if err != nil {
			t.Fatalf("Decode error: %v", err)
		}
		t.Logf("obj: %#v", gotObj)
		if _, err := api.GetReference(gotObj); err != nil {
			t.Errorf("Unable to construct reference: %v", err)
		}
		if e, a := item.obj, gotObj; !reflect.DeepEqual(e, a) {
			t.Errorf("Expected %#v, got %#v", e, a)
		}
	}
	simpleStorage.fakeWatch.Stop()

	var got watchJSON
	err = decoder.Decode(&got)
	if err == nil {
		t.Errorf("Unexpected non-error")
	}
}
Esempio n. 15
0
// PublicKey requests a shadowfax server's public key. An error is returned
// if the server URL is not https.
func PublicKey(serverURL string, client *http.Client) (*sf.PublicKey, error) {
	u, err := url.Parse(serverURL)
	if err != nil {
		return nil, errgo.Mask(err, errgo.Any)
	}
	if u.Scheme != "https" {
		return nil, errgo.Newf("public key must be requested with https")
	}
	if client == nil {
		client = http.DefaultClient
	}
	req, err := http.NewRequest("GET", serverURL+"/publickey", nil)
	if err != nil {
		return nil, errgo.Mask(err)
	}

	resp, err := client.Do(req)
	if err != nil {
		return nil, errgo.Mask(err)
	}
	defer resp.Body.Close()

	dec := json.NewDecoder(resp.Body)
	var publicKeyResp wire.PublicKeyResponse
	err = dec.Decode(&publicKeyResp)
	if err != nil {
		return nil, errgo.Mask(err)
	}

	publicKey, err := sf.DecodePublicKey(publicKeyResp.PublicKey)
	if err != nil {
		return nil, errgo.Mask(err)
	}
	return publicKey, nil
}
Esempio n. 16
0
func executeLongPolling(requestURL string, commandSliceTemplate []string) error {
	request, err := http.NewRequest("GET", requestURL, nil)
	if err != nil {
		log.Panicln(err)
		return err
	}
	client := http.Client{}
	response, err := client.Do(request)
	if err != nil {
		log.Panicln(err)
		return err
	}
	byteSlice := make([]byte, bufferSize)
	for {
		n, err := response.Body.Read(byteSlice)
		if err != nil {
			log.Panicln(err)
			return err
		}

		commandSlice := append(commandSliceTemplate, string(byteSlice[:n]))
		sendEvent(commandSlice)

		fmt.Println(string(byteSlice[:n]))
		time.Sleep(checkingInterval)
	}

	return nil
}
Esempio n. 17
0
// GetCookieConfig returns settings needed to set a Crowd SSO cookie.
func (c *Crowd) GetCookieConfig() (CookieConfig, error) {
	cc := CookieConfig{}

	client := http.Client{Jar: c.cookies}
	req, err := http.NewRequest("GET", c.url+"rest/usermanagement/1/config/cookie", nil)
	if err != nil {
		return cc, err
	}
	req.SetBasicAuth(c.user, c.passwd)
	req.Header.Set("Accept", "application/xml")
	req.Header.Set("Content-Type", "application/xml")
	resp, err := client.Do(req)
	if err != nil {
		return cc, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return cc, fmt.Errorf("request failed: %s\n", resp.Status)
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return cc, err
	}

	err = xml.Unmarshal(body, &cc)
	if err != nil {
		return cc, err
	}

	return cc, nil
}
Esempio n. 18
0
func getSessionAndAppPort(url string, rPort uint16, c *C) (string, string, string) {
	var client http.Client
	var req *http.Request
	var resp *http.Response
	var err error
	var port []byte

	uri := fmt.Sprintf("http://%s:%d/sticky", url, rPort)
	req, err = http.NewRequest("GET", uri, nil)

	resp, err = client.Do(req)
	c.Assert(err, IsNil)

	port, err = ioutil.ReadAll(resp.Body)

	var session string
	var path string
	for _, cookie := range resp.Cookies() {
		if cookie.Name == "__VCAP_ID__" {
			session = cookie.Value
			path = cookie.Path
		}
	}

	return session, string(port), path
}
Esempio n. 19
0
func HandleCompletedTask(logger lager.Logger, httpClient *http.Client, taskDB db.TaskDB, task *models.Task) {
	logger = logger.Session("handle-completed-task", lager.Data{"task_guid": task.TaskGuid})

	if task.CompletionCallbackUrl != "" {
		modelErr := taskDB.ResolvingTask(logger, task.TaskGuid)
		if modelErr != nil {
			logger.Error("marking-task-as-resolving-failed", modelErr)
			return
		}

		logger = logger.WithData(lager.Data{"callback_url": task.CompletionCallbackUrl})

		json, err := json.Marshal(&models.TaskCallbackResponse{
			TaskGuid:      task.TaskGuid,
			Failed:        task.Failed,
			FailureReason: task.FailureReason,
			Result:        task.Result,
			Annotation:    task.Annotation,
			CreatedAt:     task.CreatedAt,
		})
		if err != nil {
			logger.Error("marshalling-task-failed", err)
			return
		}

		var statusCode int

		for i := 0; i < MAX_CB_RETRIES; i++ {
			request, err := http.NewRequest("POST", task.CompletionCallbackUrl, bytes.NewReader(json))
			if err != nil {
				logger.Error("building-request-failed", err)
				return
			}

			request.Header.Set("Content-Type", "application/json")
			response, err := httpClient.Do(request)
			if err != nil {
				matched, _ := regexp.MatchString("Client.Timeout|use of closed network connection", err.Error())
				if matched {
					continue
				}
				logger.Error("doing-request-failed", err)
				return
			}
			defer response.Body.Close()

			statusCode = response.StatusCode
			if shouldResolve(statusCode) {
				modelErr := taskDB.DeleteTask(logger, task.TaskGuid)
				if modelErr != nil {
					logger.Error("delete-task-failed", modelErr)
				}
				return
			}
		}

		logger.Info("callback-failed", lager.Data{"status_code": statusCode})
	}
	return
}
Esempio n. 20
0
/*
processHost processes the host and returns the overview from it
*/
func processHost(opt *options, host string) (*Overview, error) {
	prefix := "http"
	if opt.Secure == true {
		prefix = "https"
	}
	uri := prefix + "://" + host + ":" + opt.Port + "/api/overview"
	client := http.Client{}

	request, err := http.NewRequest("GET", uri, nil)
	if err != nil {
		return nil, err
	}
	request.SetBasicAuth(opt.Username, opt.Password)
	response, err := client.Do(request)
	if err != nil {
		return nil, err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}

	over := &Overview{}
	err = json.Unmarshal(body, &over)
	if err != nil {
		return nil, err
	}

	return over, nil
}
Esempio n. 21
0
// not follow redirect....
func Gethtml3(url string) {
	client := new(http.Client)

	request, _ := http.NewRequest("GET", "http://www.baidu.com", nil)
	request.Header.Add("Accept-Encoding", "gzip")

	response, _ := client.Do(request)
	defer response.Body.Close()
	for k, v := range response.Header {
		fmt.Println(k)
		fmt.Println(v)
	}

	// Check that the server actually sent compressed data
	var reader io.ReadCloser
	switch response.Header.Get("Content-Encoding") {
	case "gzip":
		fmt.Println("XXXXXXXXXX gzip")
		reader, _ = gzip.NewReader(response.Body)
		defer reader.Close()
	default:
		reader = response.Body
	}
	var s string
	if b, err := ioutil.ReadAll(reader); err == nil {
		s = string(b)
	}

	println(s)
}
Esempio n. 22
0
func callFacebookAPI(client *http.Client, url string, response interface{}) {
	fmt.Printf("Fetching %s...\n", url)
	resp := retry.Request(url, func() (*http.Response, error) {
		req, err := http.NewRequest("GET", url, nil)
		d.Chk.NoError(err)
		return client.Do(req)
	})

	msg := func() string {
		body := &bytes.Buffer{}
		_, err := io.Copy(body, resp.Body)
		d.Chk.NoError(err)
		return fmt.Sprintf("could not load %s: %d: %s", url, resp.StatusCode, body)
	}

	switch resp.StatusCode / 100 {
	case 4:
		d.Exp.Fail(msg())
	case 5:
		d.Chk.Fail(msg())
	}

	err := json.NewDecoder(resp.Body).Decode(response)
	d.Chk.NoError(err)
}
Esempio n. 23
0
// makeRequest is a wrapper function used for making DNS API requests
func (c *DNSProvider) makeRequest(method, uri string, body io.Reader) (json.RawMessage, error) {
	url := c.cloudDNSEndpoint + uri
	req, err := http.NewRequest(method, url, body)
	if err != nil {
		return nil, err
	}

	req.Header.Set("X-Auth-Token", c.token)
	req.Header.Set("Content-Type", "application/json")

	client := http.Client{Timeout: 30 * time.Second}
	resp, err := client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("Error querying DNS API: %v", err)
	}

	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted {
		return nil, fmt.Errorf("Request failed for %s %s. Response code: %d", method, url, resp.StatusCode)
	}

	var r json.RawMessage
	err = json.NewDecoder(resp.Body).Decode(&r)
	if err != nil {
		return nil, fmt.Errorf("JSON decode failed for %s %s. Response code: %d", method, url, resp.StatusCode)
	}

	return r, nil
}
Esempio n. 24
0
func FetchUrl(theurl string) string {
	var client *http.Client

	if proxy := os.Getenv("http_proxy"); proxy != `` {
		proxyUrl, err := url.Parse(proxy)
		CheckError(err)

		transport := http.Transport{
			Dial:  TimeoutDialer(5*time.Second, 5*time.Second), // connect, read/write
			Proxy: http.ProxyURL(proxyUrl),
		}

		client = &http.Client{Transport: &transport}
	} else {
		client = &http.Client{}
	}

	req, err := http.NewRequest(`GET`, theurl, nil)
	CheckError(err)

	resp, err := client.Do(req)
	CheckError(err)

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	CheckError(err)

	return string(body)
}
Esempio n. 25
0
func test() {
	var client *http.Client
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}

	client = &http.Client{tr, nil, nil, 0 * time.Second}
	req, err := http.NewRequest("GET", "", nil)
	if err != nil {
		fmt.Println("response: ", err)
		return
	}

	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("response2: ", err)
		return
	}

	reader := bufio.NewReader(resp.Body)
	defer resp.Body.Close()
	//delim := []byte("\n")
	for {
		line, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println("response3: ", err)
			return
		} else {
			fmt.Printf("resp:%s\n", line)
		}
	}

}
Esempio n. 26
0
// submitRequest uses a given client and submits the specified request.
func (c *SecureContext) submitRequest(rw http.ResponseWriter, req *http.Request, url string, client *http.Client, responseHandler ResponseHandler) {
	// Prevents lingering goroutines from living forever.
	// http://stackoverflow.com/questions/16895294/how-to-set-timeout-for-http-get-requests-in-golang/25344458#25344458
	client.Timeout = 5 * time.Second
	// In case the body is not of io.Closer.
	if req.Body != nil {
		defer req.Body.Close()
	}
	req.Close = true
	// Make a new request.
	request, _ := http.NewRequest(req.Method, url, req.Body)
	// In case the body is not of io.Closer.
	if request.Body != nil {
		defer request.Body.Close()
	}
	request.Close = true
	// Send the request.
	res, err := client.Do(request)
	if res != nil {
		defer res.Body.Close()
	}
	if err != nil {
		rw.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(rw, "unknown error. try again")
		return
	}
	// Should return the same status.
	rw.WriteHeader(res.StatusCode)
	responseHandler(&rw, res)
}
Esempio n. 27
0
func GrantVoteResponse(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
	vote = p.ByName("vote")
	//ch <- 1
	fmt.Println("Vote received:", vote)
	if vote == "true" {
		voteCount++
	}
	fmt.Println(voteCount)
	if voteCount > total/2 && state != "leader" {
		fmt.Println("Leader....")
		state = "leader"
		myDetails := MyDetails{}
		myDetails.IsLeader = true
		myDetails.IP = "localhost" //enter the IP address
		myDetails.Port = "3001"    //enter the port number
		myDeatilsInJSON, _ := json.Marshal(myDetails)
		myDetailsBuff := new(bytes.Buffer)
		err := binary.Write(myDetailsBuff, binary.BigEndian, &myDeatilsInJSON)
		if err != nil {
			fmt.Errorf("Error in request API: %v", err)
		}
		url := fmt.Sprintf("http://localhost:9999/setleader")
		client := http.Client{}
		req, _ := http.NewRequest("POST", url, myDetailsBuff)
		res, _ := client.Do(req)
		res.Body.Close()
		NotifyFollowers() //DK
	}
}
Esempio n. 28
0
func (q *Queue) notify(t *Task) error {
	if t.callback == "" {
		return nil
	}

	buf := bytes.Buffer{}

	if t.output != nil {
		if err := json.NewEncoder(&buf).Encode(t.output); err != nil {
			return err
		}
	}

	t.backoff.Reset()

	var (
		err  error
		resp *http.Response
	)

	client := http.Client{
		Timeout: 5 * time.Second,
	}

	req, err := http.NewRequest("POST", t.callback, &buf)

	if err != nil {
		panic(err)
	}

	req.Header.Set("content-type", "application/json")

	if t.token != "" {
		req.Header.Set("token", t.token)
	}

	// Attempt to notify multiple times.
	for i := 0; i < NotifyAttempts; i++ {
		resp, err = client.Do(req)

		if err != nil {
			continue
		}

		// 200 range, good to go.
		if resp.StatusCode > 200 && resp.StatusCode < 300 {
			return nil
		}

		time.Sleep(t.backoff.Duration())
	}

	// Error making the request.
	if err != nil {
		return err
	}

	// HTTP error.
	return fmt.Errorf("Notify error: %q", resp.Status)
}
Esempio n. 29
0
func main() {
	var longUrl string
	longUrl = os.Args[len(os.Args)-1]

	body := bytes.NewBufferString(fmt.Sprintf(`{"longUrl":"%s"}`, longUrl))
	request, err := http.NewRequest("POST",
		"https://www.googleapis.com/urlshortener/v1/url",
		body)

	request.Header.Add("Content-Type", "application/json")

	client := http.Client{}
	response, err := client.Do(request)

	if err != nil {
		log.Fatal(err)
	}

	outputAsBytes, err := ioutil.ReadAll(response.Body)
	response.Body.Close()

	var output apiResponse
	err = json.Unmarshal(outputAsBytes, &output)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s\n", output.Id)
}
Esempio n. 30
0
func (l *Sensors) UpdateSensor(sensorID int, sensorName string) ([]hue.ApiResponse, error) {
	url := fmt.Sprintf(getSensorURL, l.Hostname, l.Username, sensorID)
	data := fmt.Sprintf("{\"name\": \"%s\"}", sensorName)
	post_body := strings.NewReader(data)
	request, err := http.NewRequest("PUT", url, post_body)
	if err != nil {
		return nil, err
	}
	request.Header.Set("Content-Type", "application/json")
	client := http.Client{}
	response, err := client.Do(request)
	if err != nil {
		return nil, err
	}
	defer response.Body.Close()
	contents, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}
	var apiResponse []hue.ApiResponse
	err = json.Unmarshal(contents, &apiResponse)
	if err != nil {
		return nil, err
	}
	return apiResponse, err
}