简体   繁体   中英

Get JSON representation of request body as a string

I would like to take an arbitrary http.Request and get the body as a json string. I know this involves the json package, but it seems json.Decode needs a specific struct passed in by reference. How can I decode an arbitrary request body (and then stringify the result)?

func RequestBodyJsonString(r *http.Request) string {

}

Use ioutil.ReadAll to get data in byte slice then type conversion to string to get json string

bytedata, err := ioutil.ReadAll(r.Body)
reqBodyString := string(data)

An example in go playground here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM