title: R - posting a login form using RCurl
body: <p>I am new to using R to post forms and then download data off the web. I have a question that is probably very easy for someone out there to spot what I am doing wrong, so I appreciate your patience. I have a Win7 PC and Firefox 23.x is my typical browser.</p>
  
  <p>I am trying to post the main form that shows up on </p>
  
  <p><a href="http://www.aplia.com/" rel="nofollow">http://www.aplia.com/</a></p>
  
  <p>I have the following R script:</p>
  
  <pre><code>your.username &lt;- 'username'
  your.password &lt;- 'password'
  setwd( "C:/Users/Desktop/Aplia/data" )
  
  require(SAScii) 
  require(RCurl)
  require(XML)
  agent="Firefox/23.0" 
  
  options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
  curl = getCurlHandle()
  curlSetOpt(
  cookiejar = 'cookies.txt' ,
  useragent = agent,
  followlocation = TRUE ,
  autoreferer = TRUE ,
  curl = curl
  )
  
  # list parameters to pass to the website (pulled from the source html)
  params &lt;-
  list(
  'userAgent' = agent,
  'screenWidth' = "",
  'screenHeight' = "",
  'flashMajor' = "",
  'flashMinor' = "",
  'flashBuild' = "",
  'flashPatch' = "",
  'redirect' = "",
  'referrer' = "http://www.aplia.com",
  'txtEmail' = your.username,
  'txtPassword' = your.password 
      )
  
  # logs into the form
  html = postForm('https://courses.aplia.com/', .params = params, curl = curl)
  html
  
  # download a file once form is posted
  html &lt;-
  getURL(
  "http://courses.aplia.com/af/servlet/mngstudents?ctx=filename" ,
  curl = curl
  )
  html
  </code></pre>
  
  <p>But from there I can tell that I am not getting the page I want, as what is returned into html is a redirect message that appears to be asking me to login again (?):</p>
  
  <pre><code>"\r\n\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;Aplia&lt;/title&gt;\r\n\t&lt;script language=\"JavaScript\" type=\"text/javascript\"&gt;\r\n\r\n        top.location.href = \"https://courses.aplia.com/af/servlet/login?action=form&amp;redirect=%2Fservlet%2Fmngstudents%3Fctx%3Dfilename\";\r\n    \r\n\t&lt;/script&gt;\r\n&lt;/head&gt;\r\n&lt;body&gt;\r\n    Click &lt;a href=\"https://courses.aplia.com/af/servlet/login?action=form&amp;redirect=%2Fservlet%2Fmngstudents%3Fctx%3Dfilename\"&gt;here&lt;/a&gt; to continue.\r\n&lt;/body&gt;\r\n&lt;/html&gt;\r\n"
  </code></pre>
  
  <p>Although I do believe there are a series of redirects that occur once the form is posted successfully (manually, in a browser). How can I tell the form was posted correctly?</p>
  
  <p>I am quite sure that once I can get the post working correctly, I won't have a problem directing R to download the files I need (online activity reports for each of my 500 students this semester). But spent several hours working on this and got stuck. Maybe I need to set more options with the RCurl package that have to do with cookies (as the site does use cookies) ---?</p>
  
  <p>Any help so much appreciated!! I typically use R to handle statistical data so am new to these packages and functions.</p>