github twitter linkedin email rss
Using httr with JSON API's
Mar 24, 2019
2 minutes read

It is incredbly easy to make R interact with external API’s. I’ve found the httr vignette to be a great resource on how to make a wrapper to interact with them. However I’ve stumbled in one quirk that is worth mentioning.

While creating a body to a POST request message, encoded with json, I verified that my requests were being turned down by the server. The issue was related to a behavior of the jsonlite package, one of httr’s dependencies which uses auto_unbox=TRUE by default. This isn’t an issue per se but for length one vector jsonlite returned:

cat(jsonlite:::toJSON(list(message = "my string"),auto_unbox=T))
{"message":"my string"}

Whereas the API requested a boxed response from length one vectors, such the one you get without the auto_unbox=TRUE option

cat(jsonlite:::toJSON(list(message = "my string")))
{"message":["my string"]}

Reading this github issue might give a more in depth explanation.

This led me to rabbit holes such as forking httr and recompiling with auto_unbox=FALSE option, which I did. But not without breaking other requests which truly needed to be unboxed.

The solution was simpler than I thought and makes use of the function AsIs from the base package. It can be called with the I(x) synthax and changes the class of an object indicating it should be treated as is. What this does is to prevent the auto_unbox behavior on certain fields where this is undesirable, such as in the following example:

cat(jsonlite:::toJSON(list(message = "my string",mymessage = I("My other string")),auto_unbox=T))
{"message":"my string","mymessage":["My other string"]}

This approach not only did not break anything but also made my requests compatible with the server boxed length one vectors specification as required.


Back to posts


Hey, be the first who comment this article.