diff --git a/R/parse-body.R b/R/parse-body.R index 4e8d3ea58..a5198700f 100644 --- a/R/parse-body.R +++ b/R/parse-body.R @@ -192,8 +192,11 @@ parser_multi <- function() { toparse <- parse_multipart(value, boundary) # content-type detection lapply(toparse, function(x) { - if (!is.null(x$filename)) { - x$content_type <- getContentType(tools::file_ext(x$filename)) + if (is.null(x$content_type) || isTRUE(x$content_type == "application/octet-stream")) { + if (!is.null(x$filename)) { + # Guess content-type from file extension + x$content_type <- getContentType(tools::file_ext(x$filename)) + } } parseRaw(x) }) diff --git a/tests/testthat/files/.gitattributes b/tests/testthat/files/.gitattributes new file mode 100644 index 000000000..291377b6b --- /dev/null +++ b/tests/testthat/files/.gitattributes @@ -0,0 +1 @@ +*.bin binary diff --git a/tests/testthat/files/multipart-ctype.bin b/tests/testthat/files/multipart-ctype.bin new file mode 100644 index 000000000..a76b50942 --- /dev/null +++ b/tests/testthat/files/multipart-ctype.bin @@ -0,0 +1,18 @@ +-----------------------------90908882332870323642673870272 +Content-Disposition: form-data; name="file"; filename="sample.tsv" +Content-Type: text/tab-separated-values + +x y z +0.118413259 0.708780115 0.220421733 +0.7442446 0.539953502 0.02358455 +0.452216508 0.434335382 0.290591105 +0.177870351 0.329585854 0.867134948 +0.328333594 0.564684198 0.455520456 +0.680873191 0.57619709 0.015501929 +0.87209177 0.4487502 0.008787638 +0.50966466 0.958683547 0.251500478 +0.937677985 0.457557468 0.19860169 +0.352366596 0.49283506 0.161614863 +0.370598572 0.366986682 0.15623896 + +-----------------------------90908882332870323642673870272-- diff --git a/tests/testthat/test-postbody.R b/tests/testthat/test-postbody.R index 1cce315b9..4ad6c5f72 100644 --- a/tests/testthat/test-postbody.R +++ b/tests/testthat/test-postbody.R @@ -67,3 +67,10 @@ test_that("Test multipart parser", { expect_equal(attr(parsed_body[["img1"]], "filename"), "avatar2-small.png") expect_equal(parsed_body[["json"]], list(a=2,b=4,c=list(w=3,t=5))) }) + +test_that("Test multipart respect content-type", { + bin_file <- test_path("files/multipart-ctype.bin") + body <- readBin(bin_file, what = "raw", n = file.info(bin_file)$size) + parsed_body <- parseBody(body, "multipart/form-data; boundary=---------------------------90908882332870323642673870272") + expect_equal(class(parsed_body$file), "character") +})