% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/content.r
\name{content}
\alias{content}
\alias{text_content}
\alias{parsed_content}
\title{Extract content from a request.}
\usage{
content(x, as = NULL, type = NULL, encoding = NULL, ...)
}
\arguments{
\item{x}{request object}

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

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

\item{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.}

\item{...}{Other parameters parsed on to the parsing functions, if
\code{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), \code{content()} will return \code{NA}.

For "auto", a parsed R object.
}
\description{
There are currently three ways to retrieve the contents of a request:
as a raw object (\code{as = "raw"}), as a character vector,
(\code{as = "text"}), and as parsed into an R object where possible,
(\code{as = "parsed"}). If \code{as} is not specified, \code{content}
does its best to guess which output is most appropriate.
}
\details{
\code{content} currently knows about the following mime types:
\itemize{
\item \code{text/html}: \code{\link[xml2:read_xml]{xml2::read_html()}}
\item \code{text/xml}: \code{\link[xml2:read_xml]{xml2::read_xml()}}
\item \code{text/csv}: \code{\link[readr:read_delim]{readr::read_csv()}}
\item \code{text/tab-separated-values}: \code{\link[readr:read_delim]{readr::read_tsv()}}
\item \code{application/json}: \code{\link[jsonlite:fromJSON]{jsonlite::fromJSON()}}
\item \code{application/x-www-form-urlencoded}: \code{parse_query}
\item \code{image/jpeg}: \code{\link[jpeg:readJPEG]{jpeg::readJPEG()}}
\item \code{image/png}: \code{\link[png:readPNG]{png::readPNG()}}
}

\code{as = "parsed"} is provided as a convenience only: if the type you
are trying to parse is not available, use \code{as = "text"} and parse
yourself.
}
\section{WARNING}{


When using \code{content()} in a package, DO NOT use on \code{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.
}

\examples{
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)
}
\seealso{
Other response methods: 
\code{\link{http_error}()},
\code{\link{http_status}()},
\code{\link{response}()},
\code{\link{stop_for_status}()}
}
\concept{response methods}
