How to Make an HTTP POST Request in Go

Web
Send an HTTP POST request in Go using the standard library's http.Post function with JSON data.

Use http.Post from the standard library to send a POST request with JSON data.

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
)

func main() {
	data := map[string]string{"key": "value"}
	jsonData, _ := json.Marshal(data)

	resp, err := http.Post("https://api.example.com/endpoint", "application/json", bytes.NewBuffer(jsonData))
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	defer resp.Body.Close()

	fmt.Println("Status:", resp.Status)
}