Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
LTLA opened this issue Feb 8, 2022 · 1 comment
Closed

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

LTLA opened this issue Feb 8, 2022 · 1 comment

Comments

@LTLA
Copy link

LTLA commented Feb 8, 2022

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
@hadley
Copy link
Member

hadley commented Oct 31, 2023

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!

@hadley hadley closed this as completed Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants