package main import ( "log" "net/http" "io/ioutil" ) func main() { client := &http.Client{} req, err := http.NewRequest("GET", "https://jsonplaceholder.typicode.com/posts/1", nil) if err != nil { log.Fatalln(err) } resp, err := client.Do(req) if err != nil { log.Fatalln(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatalln(err) } log.Println(string(body)) }
package main import ( "log" "net/http" "bytes" ) func main() { client := &http.Client{} data := []byte(`{"title":"foo","body":"bar","userId":1}`) req, err := http.NewRequest("POST", "https://jsonplaceholder.typicode.com/posts", bytes.NewBuffer(data)) if err != nil { log.Fatalln(err) } resp, err := client.Do(req) if err != nil { log.Fatalln(err) } defer resp.Body.Close() log.Println(resp.Status) }This example sends a POST request with a JSON payload to the same JSON placeholder API and logs the response status. The "Do" method is called on the client, passing in a new request with the appropriate method, URL, and data in the request body. In both examples, the "net/http" package is used.