go - 如何在 go 中发送带有网络凭据的 https/http 请求

标签 go https beego networkcredentials

在 C# 中

var uri = new Uri("https://demo.com/");
                var encoding = new UTF8Encoding();
                var request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "POST";

                request.Credentials = new NetworkCredential(utility.commonkey, utility.commonsecrerkey);
                request.ContentType = "application/json";
                request.Accept = "application/vnd.demo+json;version=3;";
                request.ContentLength = encoding.GetByteCount(json);
                request.Headers.Add("data", json);
                HttpWebResponse response = null;

然后我只能添加这些

req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Basic sdfsfscefewfewfew")
	req.Header.Set("Accept", "application/vnd.demo+json; version=3;")

go 中的挑战是如何在 go 中添加网络凭证

最佳答案

根据我在 the C# docs 中看到的内容您所指的 key 只能是用户名和密码。您可以使用模拟 http.Requestmethod为基本身份验证设置用户名和密码。

// username has the same value as utility.commonkey
// password is the corresponding password in plain text
req.SetBasicAuth(username, password);

不要忘记从您的 Go 代码中删除以下行

req.Header.Set("Authorization", "Basic sdfsfscefewfewfew")

关于go - 如何在 go 中发送带有网络凭据的 https/http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42619977/

相关文章:

go - 范围超过结构数组的地址

go - 无法从非根文件夹运行测试

javascript - CSS 文件和 Js 文件未使用 https 在 joomla 中加载

json - 嵌套映射返回 json array[]

go - 如何在 Beego 框架中使用 Riak 连接池

google-app-engine - 在 golang ( appengine ) 上无填充的 Base64 编码/解码

qt - 如何继续关注要禁用的复选框

http - SSL 自签名证书

http - Apache : Redirect all requests to https://www. 域.tld

json - Go语言中带有结果对象的动态结构