我有一个向elasticsearch发出请求的处理程序。 我从那个请求中得到了json响应:
resp, err := http.Get(getUrl)
defer resp.Body.Close()
bodyString := ""
if resp.StatusCode == 200{
bodyBytes, err := ioutil.ReadAll(resp.Body)
checkForError(err)
bodyString = string(bodyBytes)
fmt.Fprintf(w, bodyString)
}
如何将bodyString
转换成我可以传递给http.Post的东西:
http.Post("https://httpbin.org/post", "application/json; charset=utf-8", jsonData)
答案 0 :(得分:3)
我不确定你想要取得什么,但可能会有所帮助。
bodyBytes, err := ioutil.ReadAll(resp.Body)
reader := bytes.NewReader(bodyBytes)
http.Post("https://httpbin.org/post", "application/json; charset=utf-8", reader)
//or you can do it directly
//http.Post("https://httpbin.org/post", "application/json; charset=utf-8", resp.Body)