gohttpsbeegonetworkcredentials

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


in 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;

and in go i can add only add these

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

here in go the challenge is how to add network credentials in go


Solution

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