dax90
dax90

Reputation: 1118

How to download a file using cookies with Rcurl

I'm trying to download pictures automatically from a site that needs my user and password. To keep my authentication I create an object of class "CURLHandle" that keeps my credentials. I don't understand how pass the object with cookies when I have to download.

#install_github("omegahat/RHTMLForms")
library(RHTMLForms)
library(RCurl)
require(XML)

#create connection function from login form
login<-getHTMLFormDescription("http://mysite//Login.php")  
login<-login$Login
submit<-createFunction(login)

#create section with cookiefile 
curl = getCurlHandle(cookiefile = "", verbose = TRUE)

#Log in
submit(Password=mypass,User=myuser,.curl = curl )

#now I can navigate on the site
my_page<-getURL("http://mysite/table.php?id=988", curl = curl)

#I get for the id 988 an Url png image
my_picture<-getHTMLExternalFiles(my_page)[1]
my_picture<-paste("http://mysite/",my_picture,sep="")


setwd("c:\\temp")
download.file(my_picture, 'my_pic.png', extra = 'curl')

   > trying URL '...'
   > Error in download.file(my_picture, "my_pic.png", extra = "curl") : 
   >   cannot open URL '...'

Upvotes: 3

Views: 1087

Answers (1)

jdharrison
jdharrison

Reputation: 30435

You can use getBinaryURL:

myBin <- getBinaryURL(my_picture, curl = curl)
writeBin(myBin, "my_pic.png")

Upvotes: 3

Related Questions