zoukankan      html  css  js  c++  java
  • go语言中通过http访问需要认证的api

      

    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)
    

      

  • 相关阅读:
    【经典数据结构】B树与B+树
    【经典算法】线性时间排序
    【经典算法】归并排序
    【经典算法】快速排序
    python模块之shelve
    python模块之pickle
    python模块之json
    python之序列化
    python模块之shutil和zipfile
    python模块之sys
  • 原文地址:https://www.cnblogs.com/luffe/p/8797039.html
Copyright © 2011-2022 走看看