r-lib / httr

httr: a friendly http package for R
https://httr.r-lib.org
Other
986 stars 1.99k forks source link

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

Closed LTLA closed 1 year ago

LTLA commented 2 years ago

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:

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 ```
hadley commented 1 year ago

httr has been superseded in favour of httr2, so is no longer under active development. If this problem is still important to you in httr2, I'd suggest filing an issue offer there 😄 (but httr2 generally does less munging of this sort so I doubt it's a problem). Thanks for using httr!