Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send an XML request to an API with R

Tags:

curl

r

xml

i am trying to automate the download of a bit of data from the API of this government website.

the website instructs:

All requests for this version should be made to the following URL:
http://api.finder.healthcare.gov/v2.0/

i can find plenty of information about how to send xml requests, but none of the examples are R-specific.. and there's plenty of R code out there that shows how to use the XML, httr, and RCurl packages, but i couldn't find any examples on SO or the r-help mailing list of how to actually send an xml request. ..there's more documentation for parsing the response.

on the government website, if you click the PlansForIndividualOrFamily Samples example, it displays the xml request (code below) that needs to be sent.

url <- "http://api.finder.healthcare.gov/v2.0/"

xml.request <-
    "<?xml version='1.0' encoding='UTF-8'?>
    <PrivateOptionsAPIRequest>
        <PlansForIndividualOrFamilyRequest>
            <Enrollees>
                <Primary>
                    <DateOfBirth>1990-01-01</DateOfBirth>
                    <Gender>Male</Gender>
                    <TobaccoUser>Smoker</TobaccoUser>
                </Primary>
            </Enrollees>
            <Location>
                <ZipCode>69201</ZipCode>
                <County>
                    <CountyName>CHERRY</CountyName>
                    <StateCode>NE</StateCode>
                </County>
            </Location>
            <InsuranceEffectiveDate>2012-10-01</InsuranceEffectiveDate>
        <IsFilterAnalysisRequiredIndicator>false</IsFilterAnalysisRequiredIndicator>
        <PaginationInformation>
            <PageNumber>1</PageNumber>
            <PageSize>10</PageSize>
        </PaginationInformation>
        <SortOrder>
            <SortField>OOP LIMIT - INDIVIDUAL - IN NETWORK</SortField>
            <SortDirection>ASC</SortDirection>
        </SortOrder>
        <Filter/>
        </PlansForIndividualOrFamilyRequest>
    </PrivateOptionsAPIRequest>"
like image 505
Anthony Damico Avatar asked Mar 06 '13 06:03

Anthony Damico


1 Answers

Using RCurl you do this ;

myheader=c(Connection="close", 
            'Content-Type' = "application/xml",
             'Content-length' =nchar(xml.request))
data =  getURL(url = url,
                    postfields=xml.request,
                    httpheader=myheader,
                    verbose=TRUE)

That's about it. Then You can using xpathApply of XML package to retrieve data. For example to get families ID:

library(XML)
xmltext  <- xmlTreeParse(data, asText = TRUE,useInternalNodes=T)
 unlist(xpathApply(xmltext,'//Plan/PlanID',xmlValue))  ## change the right xpath here

 "29678NE0780012" "29678NE0780011" "29678NE0140010"  
  "29678NE0780010" "29678NE0140019" "29678NE0140018" "29678NE0140017"
  "29678NE0140016" "29678NE0780005" "29678NE0780004"
like image 169
agstudy Avatar answered Oct 05 '22 08:10

agstudy