title: Calling a rest api with username and password - how to
body: <p>I am new to rest api's and calling them via .NET</p>
  
  <p>I have an api: <a href="https://sub.domain.com/api/operations?param=value&amp;param2=value">https://sub.domain.com/api/operations?param=value&amp;param2=value</a></p>
  
  <p>The notes for the api say that to authorize I need to use the basic access authentication - how do I do that?</p>
  
  <p>I currently have this code:</p>
  
  <pre><code>        WebRequest req = WebRequest.Create(@"https://sub.domain.com/api/operations?param=value&amp;param2=value");
          req.Method = "GET";
          //req.Credentials = new NetworkCredential("username", "password");
          HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
  </code></pre>
  
  <p>However I get a 401 unauthorized error.</p>
  
  <p>What am I missing, how do I form api calls using the basic access auth?</p>