Rcurl with http data post

2019-07-19 12:21发布

问题:

I would like to move the following curl call to Rcurl:

curl  'http://myserver.org/stream' 
-H 'Authorization: Basic XXXXXXXX' -H 'Connection: keep-alive' --data-binary '{"limit": 20}' 
-H 'Content-Type: application/json;charset=UTF-8'

This is one of my R tests:

library(RCurl)
url.opts <- list(httpheader = list(Authorization ="Basic XXXXXXXX", 
                Connection = "keep-alive", 
                "Content-Type" = "application/json;charset=UTF-8"))

getURLContent('http://myserver.org/stream', .opts=url.opts)

Now I am missing an attribute to add --data or --data-binary. How to add this option?

Thanks a lot Markus

回答1:

Thanks a lot @hadley.

This is my working solution:

library(httr)
user <- "test"
password <- "test123"
POST(url = 'http://myserver.org/stream', 
     config = c(authenticate(user, password, "basic"), 
                add_headers(Connection = "keep-alive"), 
                accept_json()), 
     body = "{'username':'test'}")


标签: r rcurl