2016-06-11 119 views
0

我有一个处理程序向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) 

回答

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)