// Importing the necessary package import ( "net/http" "log" ) func main() { response, err := http.Get("https://jsonplaceholder.typicode.com/posts") if err != nil { log.Fatalln(err) } defer response.Body.Close() // Do something with the response }
// Importing the necessary package import ( "net/http" "log" "bytes" ) func main() { // Create a new request with a JSON payload url := "https://jsonplaceholder.typicode.com/posts" jsonStr := []byte(`{"title":"foo","body":"bar","userId":1}`) req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) req.Header.Set("Content-Type", "application/json") // Submit the request client := &http.Client{} res, err := client.Do(req) if err != nil { log.Fatalln(err) } defer res.Body.Close() // Do something with the response }This code performs a POST request to the specified URL with a JSON payload. We create a new request using http.NewRequest() and set the request headers as necessary. We can then submit the request using an http.Client and get the response as an http.Response struct. Both examples use the "net/http" package library.