Skip to content

req_body_multipart fails when one named element is a list #451

@GreenGrassBlueOcean

Description

@GreenGrassBlueOcean

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 with req_perform)

library(httr2)

# curl -X POST -H "Content-Type: application/json" -d '{"message": "<message>", "number": "<number>",
# "recipients": ["<recipient1>", "<recipient2>"]}' 'http://127.0.0.1:8080/v2/send'

Request_url <- "http://127.0.0.1:8080/v2/send"

Request_body <-  list( 'message' = "Hello World!"
                     , 'number' =  "+1123456789"
                     , "recipients" = list("+1987654321", "+1987654322")
                     )

query <- httr2::request(base_url = Request_url) |>
  httr2::req_headers('Content-Type' = 'application/json') |>
  httr2::req_timeout(60) |>
  httr2::req_body_json(Request_body)|>
  httr2::req_dry_run()
#> POST /v2/send HTTP/1.1
#> Host: 127.0.0.1:8080
#> User-Agent: httr2/1.0.0 r-curl/5.2.0 libcurl/8.3.0
#> Accept: */*
#> Accept-Encoding: deflate, gzip
#> Content-Type: application/json
#> Content-Length: 92
#> 
#> {"message":"Hello World!","number":"+1123456789","recipients":["+1987654321","+1987654322"]}

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)

library(httr2)
library(RCurl)
library(ggplot2)

# curl -X POST -H "Content-Type: application/json" -d '{"message": "<message>"
#, "base64_attachments": ["<base64 encoded attachment>"]
# , "number": "<number>", "recipients": ["<recipient1>", "<recipient2>"]}' 'http://127.0.0.1:8080/v2/send'

p <- ggplot(mtcars, aes(wt, mpg)) + geom_point() 
ggsave(p, filename = "test.png")
base64_attachments <- "test.png"

Request_body <-  list( 'message' = "Hello World!"
                       , 'number' =  "+1123456789"
                       , "recipients" = list("+1987654321", "+1987654322")
)

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(Request_body ) |>
  httr2::req_body_multipart(base64_attachments = curl::form_file(base64_attachments))

query
#> <httr2_request>
#> POST http://127.0.0.1:8080/v2/send
#> Headers:
#> • Content-Type: 'application/json'
#> Body: multipart encoded data
#> Options:
#> • useragent: 'RSignalApi'
#> • timeout_ms: 60000
query$body
#> $data
#> $data$message
#> [1] "Hello World!"
#> 
#> $data$number
#> [1] "+1123456789"
#> 
#> $data$recipients
#> $data$recipients[[1]]
#> [1] "+1987654321"
#> 
#> $data$recipients[[2]]
#> [1] "+1987654322"
#> 
#> 
#> $data$base64_attachments
#> Form file: test.png 
#> 
#> 
#> $type
#> [1] "multipart"
#> 
#> $content_type
#> NULL
#> 
#> $params
#> list()

query |>  httr2::req_dry_run()
#> Error in curl::handle_setform(handle, .list = req$fields): Unsupported value type for form field 'recipients'.

Created on 2024-03-06 with reprex v2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    body 🕺bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions