func main() { //生成client 参数为默认 client := &http.Client{} //生成要访问的url url := "https://api.XXXXX.com/v2/users/XXXX/behavior?$user_id=XXXX" //提交请求 reqest, err := http.NewRequest("GET", url, nil) reqest.SetBasicAuth("13XXXX493","congmXXXXXXXXX493")//设置需要认证的username和password if err != nil { panic(err) } //处理返回结果 response, _ := client.Do(reqest) defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) fmt.Printf("%s ",body) }
当用post请//生成client 参数为默认
//生成client 参数为默认 client := &http.Client{} //生成要访问的url url := "https://u.XXXXX.com/open/v2/evXXXXXrv/upload_event" str :=`json格式的参数` //提交请求 strings.NewReader(str) 返回io.reader reqest, err := http.NewRequest("POST", url, strings.NewReader(str)) reqest.Header.Add("Authorization","Basic Y2U1ZGI5OTRmMzc1NGU4NDllZTkwMTA2Yjc0ZmQyZGE6ZWZiNjBjMTk0YWNjNDM1NWE5ZjFhZTBlNmNlNmM1Yzg=") if err != nil { panic(err) } //处理返回结果 response, _ := client.Do(reqest) defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) fmt.Printf("%s ",body)