golang post get 方式获取json数据
作者:互联网
func checkErr(err error) { if err != nil { fmt.Println(err) panic(err) } } // post方式获取json数据 func Post(value1, value2, value3 int) { var c map[string]interface{} // 提交数据的url url := "https://baidu.com/test" client := &http.Client{} data := fmt.Sprintf("{\"key1\":%v,\"key2\":%v,\"key3\":%v}", value1, value2, value3) req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(data))) checkErr(err) req.Header.Add("Content-Type", "application/json;charset=UTF-8") resp, err := client.Do(req) checkErr(err) defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) checkErr(err) err = json.Unmarshal(body, &c) checkErr(err) fmt.Println(c) } // get方式获取json数据 func Get() { var c map[string]interface{} url := "https://baidu.com/test" client := &http.Client{} req, err := http.NewRequest("GET", url, nil) checkErr(err) req.Header.Add("Content-Type", "application/json;charset=UTF-8") resp, err := client.Do(req) checkErr(err) defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) checkErr(err) err = json.Unmarshal(body, &c) checkErr(err) fmt.Println(c) }
标签:checkErr,err,get,url,resp,req,golang,json 来源: https://www.cnblogs.com/cqblog/p/16053475.html