1

Possible Duplicate:
Reading information from a password protected site

I have a webservice that provides data in csv form, based on the url you use to access it. i.e. http://sever.com/parameter1 returns a csv for parameter 1, http://sever.com/parameter1 returns a csv for parameter 2, etc. When I first access the site in my browser, I type in a username and password and can then access any data I want.

The problem arises when I try to import that data into R. I tried this function:

readLines('http://sever.com/parameter1')

But got the following error:

Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") : cannot open: HTTP status was '401 Unauthorized'

Of course, this is because R doesn't know to pass my username and password along with the request. How do I define these additional parameters in R? Is there any way to add a cookie to the request or something?

Thank you.

/edit: The answer here (different question wording wasn't picked up by SO) worked for me:

Reading information from a password protected site

If anyone else has any other advice, please let me know.

Community
  • 1
  • 1
Zach
  • 29,791
  • 35
  • 142
  • 201
  • 1
    What sort of authentication is being used? Apache or some PHP or similar script accepting POST or GET? – Gavin Simpson Jun 20 '11 at 18:41
  • @Gavin Simpson: The site simply asks for a username and password. Ammending my url to: `readLines('http://user:pass@sever.com/parameter1')` worked – Zach Jun 20 '11 at 18:43

1 Answers1

3

Why don't you use curl to grab the file? That way you can set http headers for username and password:

curl --user name:password http://www.example.com

There is a curl library for R

http://curl.haxx.se/libcurl/r/
Calvin Froedge
  • 16,135
  • 16
  • 55
  • 61