doulan4371 2016-04-22 13:41
浏览 3245

在golang的http请求中附加用户名和密码

what i have tried till now is like and http request formed now is:curl -X POST http://localhost:8080/v1.0

l_httpClient_ptr := http.Client{
  Timeout: timeout,
 }
 var l_resp_ptr *http.Response
 var l_resp_byte []byte
 l_resp_ptr, r_err = l_httpClient_ptr.Post(p_url_str, "text/xml; charset=utf-8", bytes.NewBufferString(p_request_str))

 l_resp_byte, r_err = ioutil.ReadAll(l_resp_ptr.Body)
 l_resp_ptr.Body.Close()
 r_response_str = string(l_resp_byte)

 return
}

Here I am able to send only request xml along with URL. And with my code I don't know how to send Username and password along with url that is it should be like following URL:

curl -H "ChannelName: guest" -H "ChannelPassword: guest" -X POST http://localhost:8080/v1.0/
  • 写回答

1条回答 默认 最新

  • duanshang7007 2016-04-22 15:26
    关注

    You can create a request with custom headers,like this:

    req, err := http.NewRequest("POST", p_url_str, bytes.NewBufferString(p_request_str))
    req.Header.Set("ChannelName", "guest")
    req.Header.Set("ChannelPassword", "guest") 
    

    then execute this request with:

    l_resp_byte, r_err := l_httpClient_ptr.Do(req)
    
    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条