-
Notifications
You must be signed in to change notification settings - Fork 59
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
req_body_multipart fails when one named element is a list #451
Comments
Somewhat more minimal reprex: library(httr2)
path <- tempfile()
png(path)
plot(1:10)
dev.off()
#> quartz_off_screen
#> 2
body <- list(
'message' = "Hello World!",
'number' = "+1123456789",
"recipients" = list("+1987654321", "+1987654322")
)
request("http://127.0.0.1:8080/v2/send") |>
req_body_json(body) |>
req_dry_run(quiet = TRUE)
request("http://127.0.0.1:8080/v2/send") |>
req_body_json(body) |>
req_body_multipart(base64_attachments = curl::form_file(path)) |>
req_dry_run(quiet = TRUE)
#> Error in curl::handle_setform(handle, .list = req$fields): Unsupported value type for form field 'recipients'. Created on 2024-03-07 with reprex v2.1.0 |
Oh hmmm, the problem is really that |
Hi Hadley, Many thanks for your response and I am sorry that I missed your second post somehow. Downside is that I now will receive the same attachment two times in case of a single attachment. Otherwise the generated curl command will not contain the square brackets for recipients and also attachments:
The unit tests still need to be written using the r package webfakes. The requested documentation can be found here: curl coded examples can be found here: the example for sending a message with a base 64 example is: I wrote the following implementation for r. SendMessage <- function( message = NULL, number = Sys.getenv(".SignalServerPhoneNumber")
, recipients = NULL, attachment_path = NULL){
if(is.null(message)){
stop("Message cannot be NULL")
}
if(is.null(recipients) | !is.vector(recipients)){
stop("recipients can not be NULL and has to be a vector")
}
if(length(recipients) == 1){
recipients <- rep_len(recipients, length.out = 2)
}
# Here I create a single list with all parameters for the message
json <- list( 'message' = message
, 'number' = number
, "recipients" = recipients
, "base64_attachments" = attachment_path
)
json[sapply(json, is.null)] <- NULL
query <- ExecuteRequest( Request_url = paste0(GetBaseUrl(),"v2/send")
, JsonBody = json
, request_type = "POST"
)
return(query)
} Created on 2024-03-12 with reprex v2.1.0 ExecuteRequest <- function(Request_url = NULL, JsonBody = NULL, request_type = NULL){
if(toupper(request_type) == "POST"){
if(any(is.null(Request_url), is.null(JsonBody))){
stop("Request_url or JsonBody cannot be null")
}
if("base64_attachments" %in% names(JsonBody)){
encode_file_to_base64 <- function(file_path) {
file_content <- RCurl::base64Encode(readBin(file_path, "raw", file.info(file_path)$size))
return(as.character(file_content))
}
if(length(JsonBody$base64_attachments)==1L){
JsonBody$base64_attachments <- rep_len(JsonBody$base64_attachments, length.out = 2)
}
JsonBody$base64_attachments <- lapply( X = JsonBody$base64_attachments
, FUN = encode_file_to_base64
) |> unlist()
}
query <- httr2::request(base_url = Request_url) |>
httr2::req_headers('Content-Type' = 'application/json') |>
httr2::req_user_agent("RSignalApi") |>
httr2::req_timeout(60) |>
httr2::req_body_json(JsonBody) |>
httr2::req_perform()
} else {
stop("not implemented request_type")
}
return(query)
} Created on 2024-03-12 with reprex v2.1.0 As can be observed from above I would personally like to be able to send a request using req_body_json that contains base64 files without the added complexity of choosing if I have a req_body_multipart or not. I guess that in almost all cases in which attachments are send other data is in the same body of the POST message. |
I think you just need |
I am writing an r package that will work with https://github.com/bbernhard/signal-cli-rest-api
so that one can send messages using Signal. I am planning to make this open source once finished.
To send a message to (multiple) recipients the api expects a list of multiple character numbers.
This works fine as long as I don't add an Base 64 attachment:
Without attachment works (and I also receive the message in signal when I replace
req_dry_run
withreq_perform
)Created on 2024-03-06 with reprex v2.1.0
With attachment it does not work anymore because it wants that recipients should be a named list.
However this is not supported by the api (when I remove the list, it will return a 400 error from the api)
Created on 2024-03-06 with reprex v2.1.0
The text was updated successfully, but these errors were encountered: