demo icon indicating copy to clipboard operation
demo copied to clipboard

为啥MiscDemo/stream_load没有golang的

Open timeisgoing opened this issue 1 year ago • 0 comments

我来贡献一个

func TestLoad2(t *testing.T) { //loadUrl := "http://ip:8040/api/db/test/_stream_load"
loadUrl := "http://ip:8030/api/db/test/_stream_load"
jsonData := strings.Join([]string{ {"dt": "2024-01-01 00:00:00", "id": "3", "msg": "test_message1938", "status": "success"}, {"dt": "2024-01-01 00:00:01", "id": "4", "msg": "another_msg1938", "status": "failed"}, }, "\n") req, err := http.NewRequest("PUT", loadUrl, bytes.NewBufferString(jsonData)) if err != nil { panic(fmt.Sprintf("创建请求失败: %v", err)) } auth := "appinsight:Appinsight_52tt" basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) req.Header.Set("Authorization", basicAuth) req.Header.Set("Expect", "100-continue") req.Header.Set("format", "json") req.Header.Set("label", GenerateRandomID()) req.Header.Set("Content-Type", "application/json") var redirectHost []string client := &http.Client{ Timeout: 10 * time.Minute, CheckRedirect: func(req *http.Request, via []*http.Request) error { if auth := via[0].Header.Get("Authorization"); auth != "" { req.Header.Set("Authorization", auth) } if len(via) >= 10 { log.Printf("Too many redirects, stopping at %s", req.URL.String()) return http.ErrUseLastResponse } redirectHost = append(redirectHost, req.URL.Host) return nil }, } resp, err := client.Do(req) if err != nil { panic(fmt.Sprintf("请求发送失败: %v,redirectHost:%s", err, strings.Join(redirectHost, ","))) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { panic(fmt.Sprintf("读取响应失败: %v,redirectHost:%s", err, strings.Join(redirectHost, ","))) } fmt.Printf("redirectHost:%s,HTTP状态码: %d\n响应内容:\n%s\n", strings.Join(redirectHost, ","), resp.StatusCode, body) } func GenerateRandomID() string { const charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" var segmentLengths = [5]int{8, 4, 4, 4, 12} result := make([]string, 5) for i, length := range segmentLengths { segment := make([]byte, length) if _, err := rand.Read(segment); err != nil { return fmt.Sprintf("%x-%x-%x-%x-%x", time.Now().UnixNano(), time.Now().Unix()&0xffff, time.Now().Unix()>>16, time.Now().Unix()&0xffff, time.Now().UnixNano()&0xffffff) } for j := range segment { segment[j] = charset[int(segment[j])%len(charset)] } result[i] = string(segment) } return strings.Join(result, "-") }

timeisgoing avatar Apr 25 '25 03:04 timeisgoing