[BioC] Using RCurl and XML to access an API

Martin Morgan mtmorgan at fhcrc.org
Sun Sep 13 21:37:44 CEST 2009


Hi Aaron --

Aaron Wolen wrote:
> I'd like to access an API from within R but I'm not sure how to do so. The
> API uses HTTP and supports both POST and PUT methods. I'm able to access it
> successfully from the terminal using the following curl command:
> curl --data @genes.xml http://api-url

> 
> Where genes.xml is a local file containing the genes I want to submit and
> http://api-url is the API's address. However, I'm not sure how to replicate
> this command in R. The request payload and response payload are both XML
> streams, so I'm fairly certain this will require a combination of the RCurl
> and XML packages, but I've had no luck getting RCurl to work. I'd really
> appreciate some help with this.

To get more help you'll probably have to provide more information about
what specifically is at the other end of the curl request, especially a
reproducible (as in, a real url and appropriate data argument, and an
indication of what you'd like back, e.g., as from the command line) example.

I think you want to end up using

  getURL("http://api-url", ...)

where ... are arguments that capture the command line --data @genes.xml.
The place to look for ... is in the 'man' page for curl_easy_setopt,
where an option like CURLOPT_READFUNCTION becomes a named argument
'readfunction'. I'd guess perhaps

  reader <- function(file) {
    function() paste(readLines(file), collapse="\n")
  }

  res <- getURL("http://api-url", readfunction=reader("genes.xml"))

If the return value is XML or HTML, you might then

  xml <- xmlTreeParse(res, useInternal=TRUE)

and the query xml with the xpath language, described here
http://www.w3.org/TR/xpath, especially section 2.5, abbreviated syntax.

Hope that provides some help; if you provide something to explore
against, you might get some more explicit help.

Martin

> Thanks,
> Aaron
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/bioconductor
> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor



More information about the Bioconductor mailing list