forked from r-lib/httr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.Rd
79 lines (69 loc) · 2.8 KB
/
content.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
% Generated by roxygen2 (4.0.2): do not edit by hand
\name{content}
\alias{content}
\alias{parsed_content}
\alias{text_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 auto outputs.}
\item{...}{Other parameters parsed on to the parsing functions, if
\code{as = "auto"}}
}
\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[XML]{htmlTreeParse}}
\item \code{text/xml}: \code{\link[XML]{xmlTreeParse}}
\item \code{text/csv}: \code{\link{read.csv}}
\item \code{text/tab-separated-values}: \code{\link{read.delim}}
\item \code{application/json}: \code{\link[jsonlite]{fromJSON}}
\item \code{application/x-www-form-urlencoded}: \code{parse_query}
\item \code{image/jpeg}: \code{\link[jpeg]{readJPEG}}
\item \code{image/png}: \code{\link[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{stop_for_status}},
\code{\link{warn_for_status}}; \code{\link{http_status}};
\code{\link{response}}; \code{\link{url_ok}};
\code{\link{url_success}}
}