diff --git a/DESCRIPTION b/DESCRIPTION index ddabb478a..37137b506 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,4 +1,3 @@ -Encoding: UTF-8 Package: plumber Type: Package Title: An API Generator for R @@ -45,6 +44,7 @@ Remotes: rstudio/swagger, rstudio/httpuv, sckott/analogsea +Encoding: UTF-8 Collate: 'async.R' 'content-types.R' diff --git a/NEWS.md b/NEWS.md index 4e6aae8e6..ba964b09f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,5 @@ +* Multipart input is not parsed for query strings (#504, @krlmlr). + plumber 0.5.0 -------------------------------------------------------------------------------- ## Full changelog diff --git a/R/content-types.R b/R/content-types.R index a3730d0dc..90f8c7b90 100644 --- a/R/content-types.R +++ b/R/content-types.R @@ -56,6 +56,9 @@ getCharacterSet <- function(contentType){ if (is.null(contentType)) { return(default) } + if(grepl("^multipart/", contentType)) { + return("bytes") + } charsetStart <- attr( gregexpr(".*charset=(.*)", contentType, perl = T)[[1]], "capture.start" diff --git a/R/post-body.R b/R/post-body.R index 29a3c24e2..f439bc5c1 100644 --- a/R/post-body.R +++ b/R/post-body.R @@ -15,7 +15,7 @@ postBodyFilter <- function(req){ parseBody <- function(body, charset = "UTF-8"){ # The body in a curl call can also include querystring formatted data # Is there data in the request? - if (is.null(body) || length(body) == 0 || body == "") { + if (is.null(body) || length(body) == 0 || body == "" || charset == "bytes") { return(list()) } @@ -28,7 +28,7 @@ parseBody <- function(body, charset = "UTF-8"){ ret <- safeFromJSON(body) } else { # If not handle it as a query string - ret <- parseQS(body) + ret <- parseQS(body) } ret } diff --git a/tests/testthat/test-postbody.R b/tests/testthat/test-postbody.R index 62a4743db..c9cb8145a 100644 --- a/tests/testthat/test-postbody.R +++ b/tests/testthat/test-postbody.R @@ -4,6 +4,12 @@ test_that("JSON is consumed on POST", { expect_equal(parseBody('{"a":"1"}'), list(a = "1")) }) +test_that("bytes are ignored on POST", { + body <- "dummy" + Encoding(body) <- "bytes" + expect_equal(parseBody(body), list()) +}) + test_that("Query strings on post are handled correctly", { expect_equivalent(parseBody("a="), list()) # It's technically a named list() expect_equal(parseBody("a=1&b=&c&d=1"), list(a="1", d="1"))