r - Copying cookie for httr -


i'm trying access site sets cookie on basis of type of visitor:

library(httr) url <- "https://www.blackrock.com/ca/individual/en/products/product-list#categoryid=1&lvl2=overview" 

essentially i've tried returns this...:

get(url)

response [https://www.blackrock.com/ca/individual/en/site-entry?targeturl=%2fca%2findividual%2fen%2fproducts%2fproduct-list%3fsiteentrypassthrough%3dtrue]   status: 200   content-type: text/html;charset=utf-8   size: 270 kb <!doctype html><html xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#" lang="en" xml:lang="en"><head><meta content="text/html;charset=utf-8" http-equiv="content-type... <meta name="description" content="" /> <meta name="robots" content="noindex,noarchive,nocache" /> <meta property="og:title" content="site entry" /> <meta property="og:type" content="website" /> <meta property="og:image" content="/amer-retail-assets/include/common/images/blackrock_logo.png" /> <meta property="og:site_name" content="blackrock" /> <meta property="og:locale" content="en_ca" /> <meta property="og:url" content="https://www.blackrock.com/ca/individual/en/site-entry?locale=en_ca" /> <link rel="canonical" href="https://www.blackrock.com/ca/individual/en/site-entry?locale=en_ca" /> 

so, instead tried copying cookie after manually passing gateway or site-entry page in chrome:

cookie = c(`jsession_amer-retail01` = "ac91c17f269d8f6a136e576531fa6e05", `unicaid` = "10.39.18.249-1408392737241856", `unicaniodid` = "nqhknkqykdm-yxueygx", `_ga` = "ga1.2.127078965.1408392737", `blkusertype-ca-one` = "institutional", `s_pers` = "%20s_fid%3d14569e53c0f4ee51-3982590542b2dda4%7c1471566685393%3b%20gpv%3dca-one%257cproducts%257cproduct%2520list%7c1408410085400%3b%20s_nr%3d1408408285405-repeat%7c1439944285405%3b%20s_pers_evar15%3dprospect%7c1411000285408%3b%20s_pers_prop19%3danonymous%7c1411000285411%3b", `s_sess` = "%20s_cc%3dtrue%3b%20s_sq%3d%3b",  `s_vi` = "[cs]v1|29f92f108507b6c6-6000010a8000e2ed[ce]", `ts-ca-one-locale` = "en_ca") 

get(url, set_cookies(.cookies = cookie), user_agent("mozilla/5.0"))

this returns same result.

however, when @ examples httr think results i'm getting setting cookie incorrect.

for example:

> example(set_cookies)

st_cks> set_cookies(a = 1, b = 2) config:  list of 1  $ cookie:"a1=;b2="  st_cks> set_cookies(.cookies = c(a = "1", b = "2")) config:  list of 1  $ cookie:"a1=;b2="  st_cks> get("http://httpbin.org/cookies") response [http://httpbin.org/cookies]   status: 200   content-type: application/json   size: 19 b {   "cookies": {}  st_cks> get("http://httpbin.org/cookies", set_cookies(a = 1, b = 2)) response [http://httpbin.org/cookies]   status: 200   content-type: application/json   size: 50 b {   "cookies": {     "a1": "",      "b2": ""   } 

i have expected httpbin.org return = 1 , b = 1 instead of a1 = , b2 =. doing wrong?

you're not doing wrong (at least imo). taking @ set_cookies, there's bit of code there:

cookie <- paste0(names(cookies), cookies_str, sep = "=", collapse = ";") 

which not working intended (i.e. it's turning a = "1" a1= instead of a=1).

you can use one:

sc2 <- function (..., .cookies = character(0)) {    cookies <- c(..., .cookies)   stopifnot(is.character(cookies))   cookies_str <- vapply(cookies, rcurl::curlescape, fun.value = character(1))   cookie <- paste(names(cookies), cookies_str, sep = "=",                     collapse = ";")   config(cookie = cookie)  }  get("http://httpbin.org/cookies", sc2(a = 1, b = 2))  ## response [http://httpbin.org/cookies] ##   status: 200 ##   content-type: application/json ##   size: 50 b ## { ##   "cookies": { ##     "a": "1",  ##     "b": "2" ##   } 

that uses paste instead of paste0 temporarily until that's fixed (i'll file issue on httr github repo).


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -