Convert curl code into R via the RCurl package?

前端 未结 2 1004
不知归路
不知归路 2021-01-31 23:51

How I would write the following curl POST method using R and the RCurl package?

curl -k -u myusername:mypassword -d \'{\"text\":\"Hello World!\",\"level\":\"Noob         


        
2条回答
  •  长发绾君心
    2021-01-31 23:51

    In RCurl, there's a function called postForm(), which is what you'd want to use. My generic example:

    library(RCurl)
    
    param1 <- "json"
    param2 <- "all"
    param3 <- "j3kn1kn41" # Hypothetical API token
    
    webobj <- postForm("http://www.thewebsite.com/api/",type=param1,records=param2,token=param3)
    

    Basically, you just pass any parameters you need as named arguments to postForm(), along with the base URL, and it constructs it and posts it, and puts the response into whatever object you declare (webobj) in this case.

    If you need to set any curl options, you can use .opts=curlOptions(OPTIONS HERE) to change them.

提交回复
热议问题