0

TL;DR: See title

Details:

I'm currently querying an XML API that's a little quirky.

Sometimes when you make a request, you get back an XML file of the data requested. Other times, you get back an XML file with a message that says your request has been queued.

I've thought about using xpath queries to see if the data I'm expecting is there before continuing on, but it's entirely possible that a valid, properly served query will result in an empty set so this will sometimes yield a false negative.

One saving grace is that the server will return a different HTTP status code when it is queueing the request. However, I don't know how to inspect the result of xml_read to tell what the code is and the documentation does not seem to provide any guidance.

Zelbinian
  • 3,221
  • 5
  • 20
  • 23

1 Answers1

1

Something like this may help. The idea is to get the document and check the response code, then provide the document to the XML api How to determine if a url object in R base package returns '404 Not Found'? . BTW, did you mean read_xml from xml2 package or some other package?

Community
  • 1
  • 1
Andrew Lavers
  • 4,328
  • 1
  • 12
  • 19
  • Using xml2. I had thought of that, actually, but I had trouble using read_xml on what the GET function returned. I'll dig into that, I guess. – Zelbinian Apr 14 '17 at 23:53
  • ??httr:quickstart will show you the basics. Sonething like, get a response object with r <- GET('yoururl'). This is a structured object -- not just text. You can then access the parts with status(r) to check the status, and content(r) to get the text that you will pass to read_xml – Andrew Lavers Apr 15 '17 at 00:35
  • That doesn't actually work. When you try to pass the result of `content` to `read_xml` you get `no applicable method for 'read_xml' applied to an object of class "c('XMLInternalDocument', 'XMLAbstractDocument')"` However, if you call `read_xml(r$content)` this works. – Zelbinian Apr 16 '17 at 01:57