I’m trying to get up to speed on http in R, specifically using the httr and curl packages
Question1: in httr or curl, is there a way to view the request object itself, independent of it being sent/made/executed? It would be nice to be able to do so in order to better understand what is happening under the hood.
More specific question motivating the above:
Are the following two code block examples doing identical things?
##Block 1
x <- httr::POST(url = "http://httpbin.org/post", body = "foo", content_type("application/json"))
#result 1
cat(rawToChar(x$content))
##Block 2
h <- curl::new_handle()
curl::handle_setopt(h, copypostfields = "foo")
curl::handle_setheaders(h, "Content-Type" = "application/json")
y <- curl::curl_fetch_memory("http://httpbin.org/post", handle = h)
#result 2
cat(rawToChar(y$content))
While I don't have an answer to all of your questions, I'd recommend looking at the code itself to get a better sense of what's going on "under the hood" (this is where all that talk of literate programming and having the documentation integrated with the code comes in handy!)
The httr request.R is a good example. Just excerpting from the middle of the function definition there gives you a little sense of the structure:
Thank you for that advice. Into the source code I’ll go...
Still, if there’s a straightforward way to inspect the request, I’m open to hearing it OR learning why that’s trivial/ redundant/ not already an offered stand-alone fn in httr:: or curl::.
I have to be honest, I have a bit of a gripe about this. I'd like to be able to treat the request as an object before I send it. When working with APIs, I often use Postman, which is great for that. Don't get me wrong, I lean pretty heavily on httr, but the request object is a bit opaque.
You can always build your own httr request objects with httr:::request_build (and httr:::request_perform), but you lose the help that comes with httr's wrappers, like handle re-use (e.g. for cookie management), configs that are translated to curl options, etc.
Hadn't noticed request_build, though I tend to shy away from using ::: functions from a package. I assume (appropriately?) that the package maintainer had a good reason not to export something.