Extract content from a request.

There are currently three ways to retrieve the contents of a request: as a raw object (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, ...)
request object
desired type of output: raw, text or parsed. content attempts to automatically figure out which one is most appropriate, based on the content-type.
MIME type (aka internet media type) used to override the content type returned by the server. See for a list of common types.
For text, overrides the charset or the Latin1 (ISO-8859-1) default, if you know that the server is returning the incorrect encoding as the charset in the content-type. Use for text and auto outputs.
Other parameters parsed on to the parsing functions, if as = "auto"

content currently knows about the following mime types:

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.


When using 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.

See Also

Other response methods: http_error, stop_for_status, warn_for_status; http_status; response; url_ok; url_success

  • content
  • parsed_content
  • text_content
r <- 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(""))
plot(0:1, 0:1, type = "n")
rasterImage(rlogo, 0, 0, 1, 1)
Documentation reproduced from package httr, version 0.3, License: MIT + file LICENSE

Community examples

Looks like there are no examples yet.