The goal of this document is to get you up and running with httr as quickly as possible. httr is designed to map closely to the underlying http protocol. I’ll try and explain the basics in this intro, but I’d also recommend “HTTP: The Protocol Every Web Developer Must Know” or “HTTP made really easy”.
This vignette (and parts of the httr API) derived from the excellent “Requests quickstart guide” by Kenneth Reitz. Requests is a python library similar in spirit to httr.
There are two important parts to http: the request, the data sent to the server, and the response, the data sent back from the server. In the first section, you’ll learn about the basics of constructing a request and accessing the response. In the second and third sections, you’ll dive into more details of each.
To make a request, first load httr, then call
library(httr) <- GET("http://httpbin.org/get")r
This gives you a response object. Printing a response object gives you some useful information: the actual url used (after any redirects), the http status, the file (content) type, the size, and if it’s a text file, the first few lines of output.
You can pull out important parts of the response with various helper methods, or dig directly into the object:
status_code(r) headers(r) str(content(r))
httpbin.org throughout this introduction. It
accepts many types of http request and returns json that describes the
data that it received. This makes it easy to see what httr is doing.
As well as
GET(), you can also use the
DELETE() verbs. You’re probably most
GET() is used by your browser when requesting a page, and
POST() is (usually) used when submitting a form to a
DELETE() are used most often by web APIs.
The data sent back from the server consists of three parts: the status line, the headers and the body. The most important part of the status line is the http status code: it tells you whether or not the request was successful. I’ll show you how to access that data, then how to access the body and headers.
The status code is a three digit number that summarises whether or
not the request was successful (as defined by the server that you’re
talking to). You can access the status code along with a descriptive
<- GET("http://httpbin.org/get") r # Get an informative description: http_status(r) # Or just access the raw code: $status_coder
A successful request always returns a status of 200. Common errors are 404 (file not found) and 403 (permission denied). If you’re talking to web APIs you might also see 500, which is a generic failure code (and thus not very helpful). If you’d like to learn more, the most memorable guides are the http status cats.
You can automatically throw a warning or raise an error if a request did not succeed:
I highly recommend using one of these functions whenever you’re using httr inside a function (i.e. not interactively) to make sure you find out about errors as soon as possible.
There are three ways to access the body of the request, all using
content(r, "text") accesses the body as a character
<- GET("http://httpbin.org/get") r content(r, "text")
httr will automatically decode content from the server using the
encoding supplied in the
content-type HTTP header.
Unfortunately you can’t always trust what the server tells you, so you
can override encoding if needed:
content(r, "text", encoding = "ISO-8859-1")
If you’re having problems figuring out what the correct encoding
should be, try
For non-text requests, you can access the body of the request as a raw vector:
This is exactly the sequence of bytes that the web server sent, so this is the highest fidelity way of saving files to disk:
<- content(r, "raw") bin writeBin(bin, "myfile.txt")
httr provides a number of default parsers for common file types:
# JSON automatically parsed into named list str(content(r, "parsed"))
?content for a complete list.
These are convenient for interactive usage, but if you’re writing an API wrapper, it’s best to parse the text or raw content yourself and check it is as you expect. See the API wrappers vignette for more details.
Access response headers with
This is basically a named list, but because http headers are case insensitive, indexing this object ignores case:
Like the response, the request consists of three pieces: a status
line, headers and a body. The status line defines the http method (GET,
POST, DELETE, etc) and the url. You can send additional data to the
server in the url (with the query string), in the headers (including
cookies) and in the body of
A common way of sending simple key-value pairs to the server is the
query string: e.g.
allows you to provide these arguments as a named list with the
query argument. For example, if you wanted to pass
http://httpbin.org/get you could do:
<- GET("http://httpbin.org/get", r query = list(key1 = "value1", key2 = "value2") )content(r)$args
NULL elements are automatically dropped from the
list, and both keys and values are escaped automatically.
<- GET("http://httpbin.org/get", r query = list(key1 = "value 1", "key 2" = "value2", key2 = NULL)) content(r)$args
You can add custom headers to a request with
<- GET("http://httpbin.org/get", add_headers(Name = "Hadley")) r str(content(r)$headers)
content(r)$header retrieves the headers that
headers(r) gives the headers that it sent
back in its response.)