如何在 go 中使用网络凭据发送 https/http 请求

how to send https/http request with network credentials in go

在 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 what you're referring at as keys, could only be username and password. You could use the analogue http.Request's method 中看到的设置基本身份验证的用户名和密码。

// 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")