
Last chance! 50% off unlimited learning
Sale ends in
as = "raw"
), as a character vector,
(as = "text"
), and as parsed into an R object where possible,
(as = "parsed"
). If as
is not specified, content
does its best to guess which output is most appropriate.content(x, as = NULL, type = NULL, encoding = NULL, ...)
raw
, text
or
parsed
. content
attempts to automatically figure out
which one is most appropriate, based on the content-type.as = "parsed"
For "text", a character vector of length 1. The character vector is always
re-encoded to UTF-8. If this encoding fails (usually because the page
declares an incorrect encoding), content()
will return NA
.
For "auto", a parsed R object.
content()
in a package, DO NOT use on as = "parsed"
.
Instead, check the mime-type is what you expect, and then parse yourself.
This is safer, as you will fail informatively if the API changes, and
you will protect yourself against changes to httr.content
currently knows about the following mime types:text/html
:htmlTreeParse
text/xml
:xmlTreeParse
text/csv
:read.csv
text/tab-separated-values
:read.delim
application/json
:fromJSON
application/x-www-form-urlencoded
:parse_query
image/jpeg
:readJPEG
image/png
:readPNG
as = "parsed"
is provided as a convenience only: if the type you
are trying to parse is not available, use as = "text"
and parse
yourself.
http_error
,
http_status
, response
,
stop_for_status
r <- POST("http://httpbin.org/post", body = list(a = 1, b = 2))
content(r) # automatically parses JSON
cat(content(r, "text"), "\n") # text content
content(r, "raw") # raw bytes from server
rlogo <- content(GET("http://cran.r-project.org/Rlogo.jpg"))
plot(0:1, 0:1, type = "n")
rasterImage(rlogo, 0, 0, 1, 1)
Run the code above in your browser using DataLab