0

I'm trying to login Moddle and get some log files with this:

postForm('http://ead.portalinstitutounibanco.org.br/login/index.php',
                    username = "Username value",
                    password = "Password value",
                    submitButton = "Acesso",
                    style = "POST")


url <- "http://ead.portalinstitutounibanco.org.br/course/report/log/index.php?chooselog=1&showusers=1&showcourses=1&id=149&user=0&date=0&modid=&modaction=0&logformat=downloadascsv"

download.file(url, destfile = "log2.txt")

But I'm getting the login page as the txt file. What should I do?

  • I don't know rcurl, so please forgive me if this is irrelevant: does it save the login credentials when you post the form? Or is the download action effectively a new session? I seem to recall, performing a similar task with the command-line curl, having to capture a session cookie and resending it with the later requests. – Chris Throup Jun 11 '13 at 20:53
  • Hi Chris, RCurl is a libraryfor the statistical package R; the basic idea is the same. Maybe this is the problem, I will see how to create and resend the cookie. But first, you think the parameters are okay? Thanks! – user2263784 Jun 11 '13 at 23:07
  • The fields look correct; you don't need to send the submitButton, but it won't cause harm. Assuming the values are correct for your site, the report URL looks correct too. I found another question all about RCurl and cookies, so I have written a short answer for you below. – Chris Throup Jun 12 '13 at 07:45

2 Answers2

0

When you submit the login form, Moodle will set a cookie to confirm you are an authenticated user. You need to capture this cookie and send it along with your second request.

See How do I use cookies with RCurl? for how to manage cookies with RCurl.

Community
  • 1
  • 1
Chris Throup
  • 651
  • 1
  • 4
  • 19
0
handle <- getCurlHandle(cookiejar="")
postForm('http://ead.portalinstitutounibanco.org.br/login/index.php',
                username = "Username value",
                password = "Password value",
                submitButton = "Acesso",
                style = "POST",
                curl=handle                  )
url <- "http://ead.portalinstitutounibanco.org.br/course/report/log/index.php?chooselog=1&showusers=1&showcourses=1&id=149&user=0&date=0&modid=&modaction=0&logformat=downloadascsv"
getURL(url=url, curl=handle)

something like the above should work. When needing cookies to work, you have to stay within the RCurl framework. Creating a handle and specifying to use it in the first request (postForm...) AND the second (getURL...) while ensure that any cookies set as response to the first request will also be send in the second. download.file() does not support any of this functionality.

Let me know if it works because I cannot check it myself.

petermeissner
  • 12,234
  • 5
  • 63
  • 63
  • 1
    While this is generally a nice answer, it includes sending the username and password as plain text over HTTP (as opposed to using SSL). You should at least note that this presents major security risks. – Thomas Feb 28 '14 at 15:41
  • +1 for bringing in the security issue, although this was not the question right?! You are free to edit my answer to make it a better one. – petermeissner Feb 28 '14 at 16:17