package test import ( "bytes" "cicv-data-closedloop/common/util" "encoding/json" "fmt" "io/ioutil" "net/http" "net/url" "testing" ) func TestHttp(t *testing.T) { request1 := make(map[string]string) request1["equipmentNo"] = "pjibot-P1YNYD1M227000115" request1["secretKey"] = "P1YNYD1M227000115" resp1 := PostData2("http://36.110.106.156:11121/device/auth", request1) toMap, _ := util.JsonStringToMap(resp1) dataMap, _ := toMap["data"].(map[string]interface{}) token, _ := dataMap["accessToken"].(string) request2 := make(map[string]string) request2["equipmentNo"] = "pjibot-P1YNYD1M227000115" header2 := make(map[string]string) header2["token"] = token resp2 := GetData3("http://36.110.106.156:11121/device/task/poll", request2, header2) fmt.Println(resp2) } func PostData2(url string, params map[string]string) string { defer func() { if r := recover(); r != nil { fmt.Println("Recovered from panic:", r) } }() // Convert the map to JSON jsonData, err := json.Marshal(params) if err != nil { fmt.Println("Error marshaling params to JSON:", err) return "" } // Create a new HTTP POST request req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { fmt.Println("Error creating HTTP request:", err) return "" } // Set the appropriate headers req.Header.Set("Content-Type", "application/json") // Create a new HTTP client and send the request client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Error sending HTTP request:", err) return "" } defer resp.Body.Close() // Read the response body body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading response body:", err) return "" } return string(body) } func PostData3(url string, params map[string]string, headers map[string]string) string { defer func() { if r := recover(); r != nil { fmt.Println("Recovered from panic:", r) } }() // Convert the map to JSON jsonData, err := json.Marshal(params) if err != nil { fmt.Println("Error marshaling params to JSON:", err) return "" } // Create a new HTTP POST request req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { fmt.Println("Error creating HTTP request:", err) return "" } // Set the appropriate headers req.Header.Set("Content-Type", "application/json") for key, value := range headers { req.Header.Set(key, value) } // Create a new HTTP client and send the request client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Error sending HTTP request:", err) return "" } defer resp.Body.Close() // Read the response body body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading response body:", err) return "" } return string(body) } func GetData2(baseURL string, params map[string]string) string { defer func() { if r := recover(); r != nil { fmt.Println("Recovered from panic:", r) } }() // Build the URL with query parameters u, err := url.Parse(baseURL) if err != nil { fmt.Println("Error parsing URL:", err) return "" } q := u.Query() for key, value := range params { q.Set(key, value) } u.RawQuery = q.Encode() // Create a new HTTP GET request req, err := http.NewRequest("GET", u.String(), nil) if err != nil { fmt.Println("Error creating HTTP request:", err) return "" } // Create a new HTTP client and send the request client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Error sending HTTP request:", err) return "" } defer resp.Body.Close() // Read the response body body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading response body:", err) return "" } return string(body) } func GetData3(baseURL string, params map[string]string, headers map[string]string) string { defer func() { if r := recover(); r != nil { fmt.Println("Recovered from panic:", r) } }() // Build the URL with query parameters u, err := url.Parse(baseURL) if err != nil { fmt.Println("Error parsing URL:", err) return "" } q := u.Query() for key, value := range params { q.Set(key, value) } u.RawQuery = q.Encode() // Create a new HTTP GET request req, err := http.NewRequest("GET", u.String(), nil) if err != nil { fmt.Println("Error creating HTTP request:", err) return "" } // Set the appropriate headers for key, value := range headers { req.Header.Set(key, value) } // Create a new HTTP client and send the request client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Error sending HTTP request:", err) return "" } defer resp.Body.Close() // Read the response body body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading response body:", err) return "" } return string(body) }