我有一个向 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)
最佳答案
我不太确定您要实现什么目标,但可能会有所帮助。
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)
关于json - Go:如何将响应体变成请求体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37759697/