title: How do I use RestSharp to POST a login and password to an API?
body: <p>I'm trying to write a C# application that will call an api for Pacer.gov.  First thing I need to do it get a cookie to pass with my request.  I'm using Chrome's Postman app to try to generate a POST request.  Can someone explain what I'm doing wrong?</p>
  
  <p>The instructions I got from Pacer.gov to get the authentication token are</p>
  
  <blockquote>
    <p>For credentials, you can POST username(loginid) and password(passwd) to the authentication service here <a href="https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl" rel="nofollow noreferrer">https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl</a> 
    This will issue you a PacerSession cookie. </p>
  </blockquote>
  
  <p>Here are some ways I tried:
  (I’m just showing @@@@@ as the login and ***** as the password, but I used the actual login and password when I tried it)</p>
  
  <ul>
  <li><p>Including username and password in the URL</p>
  
  <pre><code>var client = new RestClient("https://pacer.login.uscourts.gov/cgi-in/check-pacer-passwd.pl?loginid=@@@@@&amp;passwd=*****");
  var request = new RestRequest(Method.POST);
  request.AddHeader("postman-token", "a5bd0d9c-85f5-4ed6-6ad8-ac06e553f5db");
  request.AddHeader("cache-control", "no-cache");
  IRestResponse response = client.Execute(request);
  </code></pre>
  
  <p>o   Received HTML page saying “Access to PACER requires a valid account id and password.  “</p></li>
  <li><p>Including username and password in the header</p>
  
  <pre><code>var client = new RestClient("https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl");
  var request = new RestRequest(Method.POST);
  request.AddHeader("postman-token", "add9dedf-10bf-eab4-428a-134c0e8a1783");
  request.AddHeader("cache-control", "no-cache");
  request.AddHeader("passwd", "*****");
  request.AddHeader("loginid", "@@@@@");
  IRestResponse response = client.Execute(request);
  </code></pre>
  
  <p>o   Received HTML page saying “Access to PACER requires a valid account id and password.  “</p></li>
  <li><p>Including username and password in the body as form data</p>
  
  <pre><code>var client = new RestClient("https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl");
  var request = new RestRequest(Method.POST);
  request.AddHeader("postman-token", "454a8ebd-b2b4-3db2-0c5c-c48e2964c671");
  request.AddHeader("cache-control", "no-cache");
  request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
  request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"loginid\"\r\n\r\n@@@@@\r\n-----011000010111000001101001\r\nContent-Disposition: form-data;          name=\"passwd\"\r\n\r\n*****\r\n-----011000010111000001101001--", ParameterType.RequestBody);
  IRestResponse response = client.Execute(request);
  </code></pre>
  
  <p>o   Received message “Could not get any response”</p></li>
  <li><p>Typing URL into Chrome with the username and password</p>
  
  <pre><code>https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl?loginid=@@@@@&amp;passwd=*****
  </code></pre>
  
  <p>o   Received error “This site can’t be reached”</p></li>
  </ul>
  
  <p>UPDATE - 5-31-16</p>
  
  <p>I've made several more attempts, but am still not getting the cookie back.  I tried this code:</p>
  
  <pre><code>        var client = new RestClient("https://pacer.login.uscourts.gov");
  
          var request = new RestRequest("/cgi-bin/check-pacer-passwd.pl", Method.POST);
          request.AddParameter("loginid", "@@@@@");
          request.AddParameter("passwd", "*****");
  
          IRestResponse response = client.Execute(request);
  </code></pre>
  
  <p>When I look at the response, there is one cookie called JESSIONID which appears just to be a generic browser session ID, not the authentication code I'm expecting:</p>
  
  <p><a href="https://i.stack.imgur.com/RtBnm.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RtBnm.jpg" alt="enter image description here"></a></p>
  
  <p>I'm not exactly sure what to expect from the response, but I know that a "NextGenCSO cookie" was mentioned in one of the e-mails from Pacer.  If I login through the web browser and look at the cookies, I see a NextGenCSO cookie; I'm guessing I should be seeing something similar in the response from the API call:</p>
  
  <p><a href="https://i.stack.imgur.com/lLhEd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lLhEd.jpg" alt="enter image description here"></a></p>
  
  <p>UPDATE - 6-3-16</p>
  
  <p>Thanks to help from @jonathon-reinhart, I was able to get the authentication cookie working with this code:</p>
  
  <pre><code>// Authentication
          var client = new RestClient("https://pacer.login.uscourts.gov");
  
          client.CookieContainer = new System.Net.CookieContainer();
          var request = new RestRequest("/cgi-bin/check-pacer-passwd.pl", Method.POST);
          request.AddParameter("loginid", "@@@@@");
          request.AddParameter("passwd", "*****");
  
          IRestResponse response = client.Execute(request);
  
          var PacerSession = "";
  
          foreach (RestResponseCookie cookie in response.Cookies)
          {
              if (cookie.Name == "PacerSession")
              {
                  PacerSession = cookie.Value;
              }
          }
  </code></pre>