go_test.go 739 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "testing"
  9. "time"
  10. )
  11. func TestA(t *testing.T) {
  12. data := map[string]interface{}{
  13. "detail": "中共潮州市委办公室",
  14. }
  15. res := getAreaInfo(data)
  16. fmt.Println(res)
  17. }
  18. func getAreaInfo(data map[string]interface{}) map[string]interface{} {
  19. info := map[string]interface{}{}
  20. client := &http.Client{Timeout: 2 * time.Second}
  21. jsonStr, _ := json.Marshal(data)
  22. resp, err := client.Post("http://127.0.0.1:9996/service/region", "application/json", bytes.NewBuffer(jsonStr))
  23. if err != nil {
  24. return info
  25. }
  26. res, err := io.ReadAll(resp.Body)
  27. if err != nil {
  28. return info
  29. }
  30. err = json.Unmarshal(res, &info)
  31. if err != nil {
  32. return info
  33. }
  34. return info
  35. }