Securely transfer any R object with full attribute preservation and cross-platform JSON compatibility. This uses the same approach as the mmbi.epi package of the UMCG.
post_data(
object,
url,
authorization_header = NULL,
compress = TRUE,
encrypt = TRUE,
key = read_secret("tools.encryption_password")
)
create_json_body(
object,
compress = TRUE,
encrypt = TRUE,
key = read_secret("tools.encryption_password")
)Any object of any size, preferably a data set
A character string specifying the target URL for the HTTP POST request. Must include the full scheme (e.g., "https://" or "http://"), hostname, and path.
A character string specifying the value of the Authorization header to include in the POST request, e.g. "Bearer <token>". Use NULL to omit the header.
Should the object be compressed/decompressed? At least allowed: "gzip" (or TRUE), "bzip2", "xz", see base::memCompress(). Use FALSE to not compress/decompress.
Should the object be encrypted/decrypted? This applies AES-GCM via openssl::aes_gcm_encrypt(), providing authenticated encryption. This guarantees both confidentiality and integrity: the file cannot be read without the correct key, and any tampering will be detected automatically during decryption. The initialization vector (iv) will be a length-12 random raw vector.
A character to be used as the encryption key. Internally, this is converted using openssl::sha256() to ensure a raw high-entropy key of length 32, suitable for AES-GCM encryption. The default is read_secret("tools.encryption_password"). If the key is missing or empty, e.g. when this secret is not set, the object will not be encrypted.
if (FALSE) { # \dontrun{
post_data(iris,
url = "https://some-server:8000/post",
compress = TRUE,
encrypt = TRUE)
} # }
# MANUAL WAY -----------------------------------------------------------
# use create_json_body() to make a(n encrypted) JSON of an object
# (the default key is read_secret("tools.encryption_password"); here, an example key is used)
iris_json <- iris |> create_json_body(compress = TRUE, encrypt = TRUE, key = "example_key")
# curl -X POST https://some-server:8000/post
# -H "Content-Type: application/json"
# -d '...'
# replace the "..." with the outcome of create_json_body():
iris_json
#> {"data":"srKv2hcfxtln8DVrPYZzfvG4QyPMVvzPMpqHs5L1e5zn0iKifa50MOAyiuWpzKzqpWwcAIhV\n7HV0vnMB0rztbxCzDBTmBkg5KybWA3QxKASm49mebsWjChkb4ReQk7P5J3bdrp2Ie5Nhw5xa\n5fW/dZ/O5mh7PPvbEUEnQGy6GHvBeobrpjfqIEqJ9WaRIpo/frrj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