[R] Using R for cURL commands,Message-ID:

Therneau, Terry M., Ph.D. therneau at mayo.edu
Mon Apr 4 15:17:33 CEST 2016



On 04/02/2016 05:00 AM, r-help-request at r-project.org wrote:
> Hello,
>
>
> I'm looking for a way in which R can make my live easier.
>
> Currently i'm using R convert data from a dataframe to json's and then sending these json's to a rest api using a curl command in the terminal (i'm on a mac).
>
>
> I've been looking for a way to use R for sending data from R to the rest api. My primairy focus was on using R for executing the curl command, however, I'm open to other approaches. The method I've been using so far:


-----------------
These are lines snipped from a working application.  I make use of the httr package.  The 
parameters of the call are assembled as a nested list "xlist" prior to this section.  The 
set of errors I check for is unique to the API I'm talking to, an internal one that's a 
bit odd in its return codes, but still it gives you the idea.

     auth <- authenticate(id$lanid, id$password, type="basic")
     query <- POST(url=control$posturl, body= xlist, auth, encode="json")

     if (status_code(query) >= 300) handle_reset(control$posturl)
     if (status_code(query) == 401)
             stop("invalid lanid/password for this application")
     else if (status_code(query) == 400)
              stop("internal error from dart package: 'syntax of request'")
     else stop_for_status(query)  #other query errors, e.g. server down



More information about the R-help mailing list