英文:
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;
而在Go中,你可以使用以下代码添加网络凭据:
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Basic sdfsfscefewfewfew")
req.Header.Set("Accept", "application/vnd.demo+json; version=3;")
在Go中添加网络凭据的挑战是什么?
英文:
in c#
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
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;
<!-- end snippet -->
and in go i can add only add these
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Basic sdfsfscefewfewfew")
req.Header.Set("Accept", "application/vnd.demo+json; version=3;")
<!-- end snippet -->
here in go the challenge is how to add network credentials in go
答案1
得分: 0
根据我在C#文档中的了解,你所提到的"keys"可能指的是用户名和密码。你可以使用类似的方法http.Request
的method来设置基本身份验证的用户名和密码。
// username的值与utility.commonkey相同
// password是对应的明文密码
req.SetBasicAuth(username, password);
同时,请确保从你的Go代码中删除以下行:
req.Header.Set("Authorization", "Basic sdfsfscefewfewfew");
英文:
From what I see in 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 to set username and password for basic auth.
// username has the same value as utility.commonkey
// password is the corresponding password in plain text
req.SetBasicAuth(username, password);
Don't forget to remove the following line from your Go code as well
req.Header.Set("Authorization", "Basic sdfsfscefewfewfew")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论