l2.go 751 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "encoding/json"
  6. "time"
  7. "bytes"
  8. "io"
  9. )
  10. type UserStruct struct{
  11. Age int `json:"age"`
  12. Name string `json:"name"`
  13. Email string `json:"email"`
  14. }
  15. func main() {
  16. user := UserStruct{
  17. Age: 66,
  18. Name: "Sergey",
  19. Email: "Likhodeev.60@mail.ru",
  20. }
  21. JsonData, err := json.Marshal(user)
  22. if err != nil {
  23. panic(err)
  24. }
  25. client := &http.Client{
  26. Timeout: 5 * time.Second,
  27. }
  28. req, err := http.NewRequest("POST", "http://127.0.0.1:7777/user", bytes.NewBuffer(JsonData))
  29. if err != nil {
  30. panic(err)
  31. }
  32. resp, err := client.Do(req)
  33. if err != nil {
  34. panic(err)
  35. }
  36. body, err := io.ReadAll(resp.Body)
  37. if err != nil {
  38. panic(err)
  39. }
  40. fmt.Printf("status code %d\n", resp.StatusCode)
  41. fmt.Println(string(body))
  42. }