9951 explained code solutions for 126 technologies


golangHow to get response body of HTTP request


package main

import ("net/http"; "os"; "io")

func main() {
  r, _ := http.Get("https://echoof.me/")
  defer r.Body.Close()
  
  b, _ := io.ReadAll(r.Body)
  body := string(b)
}ctrl + c
package main

default package declaration

net/http

http package to work with http protocol

http.Get(

executes GET request to the specified URL

io.ReadAll(r.Body)

reads entire buffer (response body) into b bytes var

string(b)

converts bytes to string and writes result to body var