想写一个简单的脚本写数据
使用php发现好多扩展要开,还要装composer
还是继续用golang吧
没想到golang的elastic客户端也是老有一些配置,比如ssl
于是换成简单的post请求
需要将 json 数组读取转字符串,然后绕过 ssl 验证、添加header、添加密码
package main import ( "bytes" "crypto/tls" "encoding/json" "io/ioutil" "log" "net/http" ) func main() { dataList := `[{"user" : "jack", "message" : "hello world"}]` var m []map[string]string if err := json.Unmarshal([]byte(dataList), &m); err != nil { panic(err) } if len(dataList) > 0 { for _, v := range m { content, err := json.Marshal(v) if err != nil { panic(err) } HttpPost(content) } } } // var a = `{"name":"jack"}` func HttpPost(content []byte) { var j = bytes.NewBuffer(content) // io.Reader 需要一个 io.Reader 对象? 从字符串中获取怎么做 - strings.NewReader("name=cjb") tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := &http.Client{Transport: tr} //提交请求 reqest, err := http.NewRequest("POST", "https://admin:123456@127.0.0.1:9200/user_list/_doc", j) if err != nil { panic(err) } //增加header选项 reqest.Header.Add("Content-Type", "application/json") resp, err := client.Do(reqest) if err != nil { panic(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } log.Println(string(body)) }
笔记一下