猿问

Go:如何将响应正文转换为请求正文?

我有一个向 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)


qq_笑_17
浏览 230回答 1
1回答

萧十郎

我不确定您要达到什么目标,但可能会有所帮助。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)
随时随地看视频慕课网APP

相关分类

Go
我要回答