Skip to content

Commit

Permalink
Deal with empty bodies better. Closes #91
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Apr 21, 2014
1 parent 46c480f commit bc90021
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# httr 0.3.0.99

* `has_content()` tells you if request has any content associated with it (#91)

* `parse(type = "auto")` returns NULL if no content associated with request
(#91).

* `with_verbose()` makes it easier to see verbose information when http
requests are made within other functions (#87).

Expand Down
4 changes: 4 additions & 0 deletions R/content-parse.r
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ parse_text <- function(content, type = NULL, encoding = NULL) {
}

parse_auto <- function(content, type = NULL, encoding = NULL, ...) {
if (length(content) == 0) {
return(NULL)
}

if (is.null(type)) {
stop("Unknown mime type: can't parse automatically. Use the type ",
"argument to specify manually.", call. = FALSE)
Expand Down
11 changes: 11 additions & 0 deletions R/content.r
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,14 @@ parsed_content <- function(x, ...) {
message("text_content() deprecated. Use parsed_content(x, as = 'parsed')")
content(x, as = "parsed", ...)
}

#' Does the request have content associated with it?
#'
#' @keywords internal
#' @export
#' @examples
#' has_content(POST("http://httpbin.org/post", body = FALSE))
#' has_content(HEAD("http://httpbin.org/headers"))
has_content <- function(x) {
length(r) == 0
}
6 changes: 6 additions & 0 deletions tests/testthat/test-content.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
context("Content")

test_that("parse content is null if no body", {
out <- content(HEAD("http://httpbin.org/headers"))
expect_equal(out, NULL)
})

0 comments on commit bc90021

Please sign in to comment.