Monday, 15 April 2013

r - Post request using cookies with cURL, RCurl and httr -



r - Post request using cookies with cURL, RCurl and httr -

in windows curl can post web request similar this:

curl --dump-header cook.txt ^ --data "rurl=http=//www.example.com/r&user=bob&password=hello" ^ --user-agent "mozilla/5.0" ^ http://www.example.com/login

with type cook.txt response similar this:

http/1.1 302 found date: thu, ****** server: microsoft-iis/6.0 server: ****** x-powered-by: asp.net x-aspnet-version: 1.1.4322 location: ****** set-cookie: cookie1=; domain=******; expires=****** ****** ****** ****** cache-control: private content-type: text/html; charset=iso-8859-1 content-length: 189

i can manually read cookie lines like: set-cookie: authcode=abc... (i script of course). can utilize authcode subsequent requests.

i trying same in r rcurl and/or httr (still don't know 1 improve task).

when try:

library(httr) post("http://www.example.com/login", body= list(rurl="http=//www.example.com/r", user="bob", password="hello"), user_agent("mozilla/5.0"))

i response similar this:

response [http://www.example.com/error] status: 411 content-type: text/html <h1>length required</h1>

by , big know 411-error , seek prepare request; not in curl, doing wrong post command.

can help me in translating curl command rcurl and/or httr?

here way create post request, maintain , reuse resulting cookies rcurl, illustration web pages when authentication required :

library(rcurl) curl <- getcurlhandle() curlsetopt(cookiejar="/tmp/cookies.txt", curl=curl) postform("http://example.com/login", login="mylogin", passwd="mypasswd", curl=curl) geturl("http://example.com/anotherpage", curl=curl)

r curl libcurl rcurl httr

No comments:

Post a Comment