How do I use RestSharp to POST a login and password to an API?

12,755

You are not correctly POSTing as the directions indicate.

Putting this:

https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl?loginid=@@@@@&passwd=*****

into the address bar is not a POST. That's a GET with query string parameters.

See the RestSharp homepage for an example on how to POST:

var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method

You want to include loginid and passwd as data ("parameters" as RestSharp calls them), not has headers.

So you should be instead doing something like this:

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);
Share:
12,755

Related videos on Youtube

boilers222
Author by

boilers222

Updated on June 13, 2022

Comments

  • boilers222
    boilers222 almost 2 years

    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?

    The instructions I got from Pacer.gov to get the authentication token are

    For credentials, you can POST username(loginid) and password(passwd) to the authentication service here https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl This will issue you a PacerSession cookie.

    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)

    • Including username and password in the URL

      var client = new RestClient("https://pacer.login.uscourts.gov/cgi-in/check-pacer-passwd.pl?loginid=@@@@@&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);
      

      o Received HTML page saying “Access to PACER requires a valid account id and password. “

    • Including username and password in the header

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

      o Received HTML page saying “Access to PACER requires a valid account id and password. “

    • Including username and password in the body as form data

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

      o Received message “Could not get any response”

    • Typing URL into Chrome with the username and password

      https://pacer.login.uscourts.gov/cgi-bin/check-pacer-passwd.pl?loginid=@@@@@&passwd=*****
      

      o Received error “This site can’t be reached”

    UPDATE - 5-31-16

    I've made several more attempts, but am still not getting the cookie back. I tried this 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);
    

    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:

    enter image description here

    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:

    enter image description here

    UPDATE - 6-3-16

    Thanks to help from @jonathon-reinhart, I was able to get the authentication cookie working with this 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;
                }
            }
    
  • boilers222
    boilers222 almost 8 years
    Thanks for the response. This is what I was trying to do in Postman, but for some reason it added the parameters as query strings. I tried your code (with the correct login/password, of course) and received this response: "The requested URL /cgi-in/check-pacer-passwd.pl was not found on this server." Any suggestions on how to fix the code?
  • Seng Cheong
    Seng Cheong almost 8 years
    @boilers222 Your original code had a typo (cgi-in instead of cgi-bin), which I copy and pasted. I've corrected the code in my answer.

Related