import ( "fmt" "io/ioutil" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { body, err := ioutil.ReadAll(r.Body) if err != nil { http.Error(w, "Bad Request", http.StatusBadRequest) return } fmt.Fprintf(w, "Request Body: %s", body) }
import ( "encoding/json" "fmt" "io/ioutil" "net/http" ) type User struct { Name string `json:"name"` Email string `json:"email"` } func handler(w http.ResponseWriter, r *http.Request) { var user User body, err := ioutil.ReadAll(r.Body) if err != nil { http.Error(w, "Bad Request", http.StatusBadRequest) return } err = json.Unmarshal(body, &user) if err != nil { http.Error(w, "Bad Request", http.StatusBadRequest) return } fmt.Fprintf(w, "User: %s, email: %s", user.Name, user.Email) }
import ( "bytes" "net/http" ) func main() { url := "https://example.com/api" data := []byte(`{"name":"John","email":"[email protected]"}`) req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) if err != nil { panic(err) } req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() }Conclusion: Based on the described examples, we can determine that the Go http Request Body package is built-in package provided by the standard `net/http` library which provides methods for handling HTTP requests and responses.