5

How would you do a SOAP request for EUR-Lex's API using R?

EUR-Lex is an EU database containing many legal acts. In the manual for their web services, they describe their SOAP system but not how to use R for it. I've tried for a while now to employ httr and RCurl but with no luck. I would like to use R instead of SOAPUI.

Does anyone here have any experience with this?

From the link below, should I define the body as follows?

body <- "<sear:searchRequest>
<sear:expertQuery>${expert query}</sear:expertQuery> <sear:page>${page}</sear:page>
<sear:pageSize>${pageSize}</sear:pageSize> <sear:searchLanguage>${search language
</sear:searchLanguage>
          </sear:searchRequest>"

How do I then combine that with the headerfields to use either the RCurl or httr package?

The following three answers seem related but I cannot figure out how to apply them to my EUR-Lex example:

  1. How to convert SOAP request curl to RCurl
  2. SOAP Client with WSDL for R
  3. SOAP request in R

EUR-Lex API links:

  1. WSDL: https://eur-lex.europa.eu/eurlex-ws?wsdl
  2. Manual: https://eur-lex.europa.eu/content/tools/webservices/SearchWebServiceUserManual_v2.00.pdf
Will M
  • 692
  • 9
  • 20
  • Update, July: I'm now attempting to use to access EUR-Lex through the EU Publications Office's SPARQL. This seems to work better in general and with R. – Will M Jul 21 '20 at 00:00

1 Answers1

1

The answers you linked to have pretty good examples to work off of. Adding in the various URLs from the WSDL and information from the manual, you end up with the code below.

Unfortunately due to the EUR-Lex security restrictions I couldn't test this (you need a username and password from them, which I assume you have), but it should at the very least get you on the right track.

library(RCurl)

headerFields =
  c(Accept = "text/xml",
    Accept = "multipart/*",
    'Content-Type' = "text/xml; charset=utf-8",
    SOAPAction = "https://eur-lex.europa.eu/EURLexWebService/doQuery")

body = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:sear="http://eur-lex.europa.eu/search">
    <soap:Header>
        <wsse:Security soap:mustUnderstand="true" xmlns:wsse="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
        <wsse:UsernameToken wsu:Id="UsernameToken-3" xmlns:wsu="http://docs.oasisopen.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:Username>${EUR-Lex username}</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-
wss-username-token-profile-1.0#PasswordText">${WS password}</wsse:Password>
        </wsse:UsernameToken>
        </wsse:Security>
   </soap:Header> 
   <soap:Body>
      <sear:searchRequest>
         <sear:expertQuery>${expert query}</sear:expertQuery>
         <sear:page>${page}</sear:page>
         <sear:pageSize>${pageSize}</sear:pageSize>
         <sear:searchLanguage>${search language}</sear:searchLanguage>
      </sear:searchRequest>
   </soap:Body>
</soap:Envelope>'

reader = basicTextGatherer()

curlPerform(url = "https://eur-lex.europa.eu/EURLexWebService",
                          httpheader = headerFields,
                          postfields = body,
                          writefunction = reader$update
                          )

xml <- reader$value()
xml
jdaz
  • 5,964
  • 2
  • 22
  • 34
  • Thanks so much! Do you know a neat way to automatically substitute certain values for the different search items in the body object, e.g. {expert query}, {page}, {pageSize} and {search language}? I'd like to be able to save for example expert_query <- "example" and then substitute it into the body object. It would be great to avoid manually editing the body object each time I use the API. I wonder if there's a method besides gsub()? – Will M Jul 08 '20 at 14:46
  • Update: Your answer helped a lot, but I'm still not quite there. My problem is now that I receive a 415 error: "Unsupported Media Type". I'll post again when I figure this out. – Will M Jul 08 '20 at 16:13
  • 1
    Hmm I'm not sure why you are getting the 415 error. For replacing the variables, though, you could try [str_glue](https://www.rdocumentation.org/packages/stringr/versions/1.4.0/topics/str_glue) – jdaz Jul 08 '20 at 18:07