예제 #1
0
파일: main.go 프로젝트: russmack/hoist
func postHttp(uri string, data string, headers map[string]string) string { // TODO: change 'data' type.

	fmt.Println("Dialing...   for post")

	tlsConfig, err := lib.GetTLSConfig(nil, cfg.SslCert, cfg.SslKey)
	if err != nil {
		log.Fatal("Error getting TLS config.", err)
	}
	tlsConfig.InsecureSkipVerify = true

	transport := http.Transport{
		Dial:                  lib.DialTimeout,
		TLSClientConfig:       tlsConfig,
		ResponseHeaderTimeout: time.Second * 45,
	}
	status := 0
	client := http.Client{
		Transport: &transport,
	}
	postBody := bytes.NewBuffer([]byte(data))
	req, err := http.NewRequest("POST", uri, postBody)
	if err != nil {
		log.Fatal("Error creating new POST request.")
	}
	// Add any headers.
	for k, v := range headers {
		req.Header.Add(k, v)
	}
	fmt.Printf("REQUEST : %+v\n", req)
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal("Error getting http resource.", err)
	} else {
		defer resp.Body.Close()
		status = resp.StatusCode
	}

	body := ""
	if status >= 200 && status < 300 {
		bodyBuf, err := lib.ReadHttpResponseBody(resp)
		if err != nil {
			fmt.Println("err reading body:", err)
			bodyStr := "{ \"success\": false, \"error\": \"" + err.Error() + "\" }"
			bodyBuf = []byte(bodyStr)
		}
		body = string(bodyBuf)
	} else {
		b, err := json.Marshal(resp)
		if err != nil {
			fmt.Println("Error marshalling to json.")
			fmt.Printf("Object to marshal : %+v\n", resp)
			body = "{ success: false, error: '" + err.Error() + "' }"
		} else {
			body = string(b)
		}
	}
	return body
}
예제 #2
0
파일: main.go 프로젝트: russmack/hoist
func deleteHttp(uri string) string {

	fmt.Println("Dialing...   for delete")

	tlsConfig, err := lib.GetTLSConfig(nil, cfg.SslCert, cfg.SslKey)
	if err != nil {
		log.Fatal("Error getting TLS config.", err)
	}
	tlsConfig.InsecureSkipVerify = true

	transport := http.Transport{
		Dial:                  lib.DialTimeout,
		TLSClientConfig:       tlsConfig,
		ResponseHeaderTimeout: time.Second * 45,
	}
	status := 0
	client := http.Client{
		Transport: &transport,
	}
	req, err := http.NewRequest("DELETE", uri, nil)
	if err != nil {
		log.Fatal("Error creating new DELETE request.")
	}
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal("Error getting http resource.", err)
	} else {
		defer resp.Body.Close()
		status = resp.StatusCode
	}

	body := ""
	if status >= 200 && status < 300 {
		bodyBuf, err := lib.ReadHttpResponseBody(resp)
		if err != nil {
			fmt.Println("err reading body:", err)
			bodyStr := "{ \"success\": false, \"error\": \"" + err.Error() + "\" }"
			bodyBuf = []byte(bodyStr)
		}
		body = string(bodyBuf)
		if strings.TrimSpace(body) == "" {
			body = "{\"StatusCode\": \"OK\"}"
		}
	} else {
		b, err := json.Marshal(resp)
		if err != nil {
			body = "{ success: false, error: '" + err.Error() + "' }"
		} else {
			body = string(b)
		}
	}
	return body
}
예제 #3
0
파일: main.go 프로젝트: russmack/hoist
func getHttpString(uri string) string {
	fmt.Println("Dialing...")

	tlsConfig, err := lib.GetTLSConfig(nil, cfg.SslCert, cfg.SslKey)
	if err != nil {
		log.Fatal("Error getting TLS config.", err)
	}
	tlsConfig.InsecureSkipVerify = true

	transport := http.Transport{
		Dial:                  lib.DialTimeout,
		TLSClientConfig:       tlsConfig,
		ResponseHeaderTimeout: time.Second * 45,
	}
	status := 0
	client := http.Client{
		Transport: &transport,
	}
	resp, err := client.Get(uri)
	if err != nil {
		body := fmt.Sprintf("{ \"success\": false, \"error\": \"Error getting http resource. %s\" }", err)
		log.Println(body)
		return body
	} else {
		defer resp.Body.Close()
		status = resp.StatusCode
	}
	body := ""
	if status >= 200 && status < 300 {
		bodyBuf, err := lib.ReadHttpResponseBody(resp)
		if err != nil {
			fmt.Println("err reading body:", err)
			bodyStr := "{ \"success\": false, \"error\": \"" + err.Error() + "\" }"
			bodyBuf = []byte(bodyStr)
		}
		body = string(bodyBuf)
	} else {
		b, err := json.Marshal(resp)
		if err != nil {
			body = "{ success: false, error: '" + err.Error() + "' }"
		} else {
			body = string(b)
		}
	}
	return body
}