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 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 https://en.wikipedia.org/wiki/Internet_media_type 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 parsed outputs. Other parameters passed 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:

• text/html: xml2::read_html()

• text/xml: xml2::read_xml()

• text/csv: readr::read_csv()

• text/tab-separated-values: readr::read_tsv()

• application/json: jsonlite::fromJSON()

• application/x-www-form-urlencoded: parse_query

• image/jpeg: jpeg::readJPEG()

• image/png: 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.

## 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.

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] "deflate, gzip" #> #>$headers$Content-Length #> [1] "228" #> #>$headers$Content-Type #> [1] "multipart/form-data; boundary=------------------------3824d7442b87926f" #> #>$headers$Host #> [1] "httpbin.org" #> #>$headers$User-Agent #> [1] "libcurl/7.58.0 r-curl/4.3 httr/1.4.2.9000" #> #>$headers$X-Amzn-Trace-Id #> [1] "Root=1-60620b03-5ed7f35a3651165527501361" #> #> #>$json
#> NULL
#>
#> $origin #> [1] "52.255.165.122" #> #>$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"
#>   },
#>     "Accept": "application/json, text/xml, application/xml, */*",
#>     "Accept-Encoding": "deflate, gzip",
#>     "Content-Length": "228",
#>     "Content-Type": "multipart/form-data; boundary=------------------------3824d7442b87926f",
#>     "Host": "httpbin.org",
#>     "User-Agent": "libcurl/7.58.0 r-curl/4.3 httr/1.4.2.9000",
#>     "X-Amzn-Trace-Id": "Root=1-60620b03-5ed7f35a3651165527501361"
#>   },
#>   "json": null,
#>   "origin": "52.255.165.122",
#>   "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] 64 65 66 6c 61 74 65 2c 20 67 7a 69 70 22 2c 20 0a 20 20 20 20 22 43 6f 6e
#> [226] 74 65 6e 74 2d 4c 65 6e 67 74 68 22 3a 20 22 32 32 38 22 2c 20 0a 20 20 20
#> [251] 20 22 43 6f 6e 74 65 6e 74 2d 54 79 70 65 22 3a 20 22 6d 75 6c 74 69 70 61
#> [276] 72 74 2f 66 6f 72 6d 2d 64 61 74 61 3b 20 62 6f 75 6e 64 61 72 79 3d 2d 2d
#> [301] 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 33 38 32
#> [326] 34 64 37 34 34 32 62 38 37 39 32 36 66 22 2c 20 0a 20 20 20 20 22 48 6f 73
#> [351] 74 22 3a 20 22 68 74 74 70 62 69 6e 2e 6f 72 67 22 2c 20 0a 20 20 20 20 22
#> [376] 55 73 65 72 2d 41 67 65 6e 74 22 3a 20 22 6c 69 62 63 75 72 6c 2f 37 2e 35
#> [401] 38 2e 30 20 72 2d 63 75 72 6c 2f 34 2e 33 20 68 74 74 72 2f 31 2e 34 2e 32
#> [426] 2e 39 30 30 30 22 2c 20 0a 20 20 20 20 22 58 2d 41 6d 7a 6e 2d 54 72 61 63
#> [451] 65 2d 49 64 22 3a 20 22 52 6f 6f 74 3d 31 2d 36 30 36 32 30 62 30 33 2d 35
#> [476] 65 64 37 66 33 35 61 33 36 35 31 31 36 35 35 32 37 35 30 31 33 36 31 22 0a
#> [501] 20 20 7d 2c 20 0a 20 20 22 6a 73 6f 6e 22 3a 20 6e 75 6c 6c 2c 20 0a 20 20
#> [526] 22 6f 72 69 67 69 6e 22 3a 20 22 35 32 2e 32 35 35 2e 31 36 35 2e 31 32 32
#> [551] 22 2c 20 0a 20 20 22 75 72 6c 22 3a 20 22 68 74 74 70 3a 2f 2f 68 74 74 70
#> [576] 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)