1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| func Get(yoururl string, data map[string]string) map[string]interface{} { request, err := http.NewRequest("GET", yoururl, nil) if err != nil { log.Println("err->", err) } q := request.URL.Query() for key, value := range data { q.Add(key, value) } request.URL.RawQuery = q.Encode() resp, err := http.DefaultClient.Do(request) if err != nil { log.Println("err->", err) } defer resp.Body.Close() data1, err := ioutil.ReadAll(resp.Body) if err != nil { log.Println("err->", err) } rdata := string(data1) result := make(map[string]interface{}) err1 := json.Unmarshal([]byte(rdata), &result) if err1 != nil { fmt.Println(err) } return result
} func Post(yoururl string, data string, yourheader map[string]string) (map[string]interface{}, *http.Response) { data2 := strings.NewReader(data) request, err := http.NewRequest("POST", yoururl, data2) for key, value := range yourheader { request.Header.Set(key, value) } if err != nil { log.Println("err->", err) } resp1, err := http.DefaultClient.Do(request) if err != nil { log.Println("err->", err) } defer resp1.Body.Close() data1, err := ioutil.ReadAll(resp1.Body) if err != nil { log.Println("err->", err) } rdata := string(data1) result := make(map[string]interface{}) err1 := json.Unmarshal([]byte(rdata), &result) if err1 != nil { fmt.Println(err) } return result, resp1 }
|