golang http request body code example

Example 1: golang read response body

//import package
import "io/ioutil"

//code snippet
    bodyBytes, err := ioutil.ReadAll(resp.Body)
    
 //---------- optioninal ---------------------
    //handling Errors
    if err != nil {
        log.Fatal(err)
    }
    
    //print result
    bodyString := string(bodyBytes)
    log.Info(bodyString)

Example 2: golang http writer redirect

func redirect(w http.ResponseWriter, r *http.Request) {
    http.Redirect(w, r, "http://www.google.com", 301)
}

Tags:

Go Example