Assigning variables to the Get HTTP request in R

2019-03-03 17:32发布

问题:

Is there any way to assign variable to a http GET request form using rcurl?

eg:

getURL("https://testme.com/www//LoginService/login?login=xyz&password=<variable>")

I need to pass the value of password as a variable.

Regards ...

回答1:

What about using paste0?

library("RCurl")
mypw <- "may1989"
basereq <- "https://testme.com/www//LoginService/login?login=xyz&password="
fullreq <- paste0( basereq, mypw, "/" )

So the full request looks like:

fullreq
## [1] "https://testme.com/www//LoginService/login?login=xyz&password=may1989/"

which you can curl in:

getURL(fullreq)


回答2:

Try httr:

library(httr)

GET("https://testme.com/www/LoginService/login", 
  query = list(login = "xyz", password = variable)
)

httr will automatically escape variable as needed.



标签: r http rcurl