httr icon indicating copy to clipboard operation
httr copied to clipboard

`httr:::body_config()` removes empty arrays from body

Open LTLA opened this issue 2 years ago • 0 comments

Some of the httr verbs call body_config() at some point, which in turn calls compact() to remove length-zero elements. I'm guessing that this was originally intended to remove NULLs, but it also removes empty vectors:

body <- list(foo=character(0), bar=NULL, whee=I("WHEE"), stuff=I(character(0)))
out <- httr:::body_config(body, "json")
cat(rawToChar(out$options$postfields))
## {"whee":["WHEE"]}

I would have hoped for foo and stuff to still be there, as they would be if I did jsonlite::toJSON manually (below).

cat(jsonlite::toJSON(body, auto_unbox=TRUE))
## {"foo":[],"bar":{},"whee":["WHEE"],"stuff":[]}

I appreciate the automatic removal of the NULL by body_config(), but the current behavior is still problematic when an empty array is a legitimate value for my endpoint. The workaround is to manually remove NULLs before JSON-ifying the body and setting encode="raw", but it took a fair amount of investigative work to nail down the issue in the first place.

So, my questions are:

  • Is this behavior (and the required workaround) documented anywhere?
  • If NULLs are the concern, would it not be better to explicitly identify/remove them with is.null in httr:::is_empty()?
Session information
R version 4.1.0 Patched (2021-06-08 r80470)
Platform: x86_64-apple-darwin19.6.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /Users/luna/Software/R/R-4-1-branch-dev/lib/libRblas.dylib
LAPACK: /Users/luna/Software/R/R-4-1-branch-dev/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] httr_1.4.2          BiocManager_1.30.16 compiler_4.1.0
[4] R6_2.5.1            tools_4.1.0         jsonlite_1.7.2

LTLA avatar Feb 08 '22 17:02 LTLA