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, ...)

Arguments

x

request object

as

desired type of output: raw, text or parsed. content attempts to automatically figure out which one is most appropriate, based on the content-type.

type

MIME type (aka internet media type) used to override the content type returned by the server. See http://en.wikipedia.org/wiki/Internet_media_type for a list of common types.

encoding

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 parsed outputs.

...

Other parameters parsed on to the parsing functions, if as = "parsed"

Value

For "raw", a raw vector.

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.

Details

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.

WARNING

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, http_status, response, stop_for_status

Examples

r <- POST("http://httpbin.org/post", body = list(a = 1, b = 2)) content(r) # automatically parses JSON
#> $args #> named list() #> #> $data #> [1] "" #> #> $files #> named list() #> #> $form #> $form$a #> [1] "1" #> #> $form$b #> [1] "2" #> #> #> $headers #> $headers$Accept #> [1] "application/json, text/xml, application/xml, */*" #> #> $headers$`Accept-Encoding` #> [1] "gzip, deflate" #> #> $headers$Connection #> [1] "close" #> #> $headers$`Content-Length` #> [1] "228" #> #> $headers$`Content-Type` #> [1] "multipart/form-data; boundary=------------------------946661a1071ea92b" #> #> $headers$Host #> [1] "httpbin.org" #> #> $headers$`User-Agent` #> [1] "libcurl/7.54.0 r-curl/2.8.1 httr/1.3.0" #> #> #> $json #> NULL #> #> $origin #> [1] "98.201.156.69" #> #> $url #> [1] "http://httpbin.org/post" #>
cat(content(r, "text"), "\n") # text content
#> No encoding supplied: defaulting to UTF-8.
#> { #> "args": {}, #> "data": "", #> "files": {}, #> "form": { #> "a": "1", #> "b": "2" #> }, #> "headers": { #> "Accept": "application/json, text/xml, application/xml, */*", #> "Accept-Encoding": "gzip, deflate", #> "Connection": "close", #> "Content-Length": "228", #> "Content-Type": "multipart/form-data; boundary=------------------------946661a1071ea92b", #> "Host": "httpbin.org", #> "User-Agent": "libcurl/7.54.0 r-curl/2.8.1 httr/1.3.0" #> }, #> "json": null, #> "origin": "98.201.156.69", #> "url": "http://httpbin.org/post" #> } #>
content(r, "raw") # raw bytes from server
#> [1] 7b 0a 20 20 22 61 72 67 73 22 3a 20 7b 7d 2c 20 0a 20 20 22 64 61 74 61 22 #> [26] 3a 20 22 22 2c 20 0a 20 20 22 66 69 6c 65 73 22 3a 20 7b 7d 2c 20 0a 20 20 #> [51] 22 66 6f 72 6d 22 3a 20 7b 0a 20 20 20 20 22 61 22 3a 20 22 31 22 2c 20 0a #> [76] 20 20 20 20 22 62 22 3a 20 22 32 22 0a 20 20 7d 2c 20 0a 20 20 22 68 65 61 #> [101] 64 65 72 73 22 3a 20 7b 0a 20 20 20 20 22 41 63 63 65 70 74 22 3a 20 22 61 #> [126] 70 70 6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e 2c 20 74 65 78 74 2f 78 6d 6c #> [151] 2c 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 6d 6c 2c 20 2a 2f 2a 22 2c 20 #> [176] 0a 20 20 20 20 22 41 63 63 65 70 74 2d 45 6e 63 6f 64 69 6e 67 22 3a 20 22 #> [201] 67 7a 69 70 2c 20 64 65 66 6c 61 74 65 22 2c 20 0a 20 20 20 20 22 43 6f 6e #> [226] 6e 65 63 74 69 6f 6e 22 3a 20 22 63 6c 6f 73 65 22 2c 20 0a 20 20 20 20 22 #> [251] 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 22 3a 20 22 32 32 38 22 2c 20 0a #> [276] 20 20 20 20 22 43 6f 6e 74 65 6e 74 2d 54 79 70 65 22 3a 20 22 6d 75 6c 74 #> [301] 69 70 61 72 74 2f 66 6f 72 6d 2d 64 61 74 61 3b 20 62 6f 75 6e 64 61 72 79 #> [326] 3d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d #> [351] 39 34 36 36 36 31 61 31 30 37 31 65 61 39 32 62 22 2c 20 0a 20 20 20 20 22 #> [376] 48 6f 73 74 22 3a 20 22 68 74 74 70 62 69 6e 2e 6f 72 67 22 2c 20 0a 20 20 #> [401] 20 20 22 55 73 65 72 2d 41 67 65 6e 74 22 3a 20 22 6c 69 62 63 75 72 6c 2f #> [426] 37 2e 35 34 2e 30 20 72 2d 63 75 72 6c 2f 32 2e 38 2e 31 20 68 74 74 72 2f #> [451] 31 2e 33 2e 30 22 0a 20 20 7d 2c 20 0a 20 20 22 6a 73 6f 6e 22 3a 20 6e 75 #> [476] 6c 6c 2c 20 0a 20 20 22 6f 72 69 67 69 6e 22 3a 20 22 39 38 2e 32 30 31 2e #> [501] 31 35 36 2e 36 39 22 2c 20 0a 20 20 22 75 72 6c 22 3a 20 22 68 74 74 70 3a #> [526] 2f 2f 68 74 74 70 62 69 6e 2e 6f 72 67 2f 70 6f 73 74 22 0a 7d 0a
rlogo <- content(GET("http://cran.r-project.org/Rlogo.jpg")) plot(0:1, 0:1, type = "n")
rasterImage(rlogo, 0, 0, 1, 1)