Missing square brackets in body of POST request using R's httr package


#1

I am using the R package httr to send a POST requests. I know how the body of the reqeust should look like, but I was not able to create it using httr. I am always missing a pair of square brackets (see below).

How do I have to modify my R code to get the desired result?

This is the R POST-snippet

cells <- c("Dimensions('Time')/Hierarchies('Time')/Elements('ABC')",
          "Dimensions('Currency')/Hierarchies('Currency')/Elements('USD')")
    value <- 123

    with_verbose(
      httr::POST(
        url = url,
        config = httr::config(ssl_verifypeer = FALSE, ssl_verifyhost = FALSE),
        body = list(Cells = list(`Tuple@odata.bind` = cells), Value = value),
        content_type("application/json"),
        encode = "json",
        set_cookies(...),
        handle = handle
      ) %>% httr::content()
    )

desired body to be sent:

 {
       "Cells":[
       {"Tuple@odata.bind":[
          "Dimensions('Time')/Hierarchies('Time')/Elements('ABC')",
          "Dimensions('Currency')/Hierarchies('Currency')/Elements('USD')"
       ]}
       ],
       "Value":"123"
    }

actual body that is sent:

{
       "Cells": ######### Missing bracket here #######
       {"Tuple@odata.bind":[ 
          "Dimensions('Time')/Hierarchies('Time')/Elements('ABC')",
          "Dimensions('Currency')/Hierarchies('Currency')/Elements('USD')"
       ]},
       ####### Missing bracket here #######
       "Value":"123"
    }

#2

It seems you need to create a more nested list. the [...] missing are for a missing list level. Cells must be a two levels list before Tuple....
Here an exemple that seems to format correctly the POST data.

cells <- c("Dimensions('Time')/Hierarchies('Time')/Elements('ABC')",
           "Dimensions('Currency')/Hierarchies('Currency')/Elements('USD')")
value <- 123
library(magrittr)
httr::POST(
  url = "http://httpbin.org/post",
  body = list(Cells = list(list(`Tuple@odata.bind` = cells)), Value = value),
  httr::content_type("application/json"),
  encode = "json"
) %>% 
  # get content as text
  httr::content(as = 'text') %>%
  # to select and print nicely
  jqr::jq(".json")
#> No encoding supplied: defaulting to UTF-8.
#> {
#>     "Cells": [
#>         {
#>             "Tuple@odata.bind": [
#>                 "Dimensions('Time')/Hierarchies('Time')/Elements('ABC')",
#>                 "Dimensions('Currency')/Hierarchies('Currency')/Elements('USD')"
#>             ]
#>         }
#>     ],
#>     "Value": 123
#> }

Created on 2018-09-07 by the reprex package (v0.2.0).

On about I build this example : I used httpbin service to test POST request. What is returned by the request is the request itself. So I get so content as text and use (awesome) jqr :package: to select the json data and print nicely.

Please try and let me know if it works !


#3

The extra list did the trick. Thanks very much!


#4

Glad it worked !

If your question's been answered, would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it: