R/httr 将列表传递给 POST 查询

R/httr pass list to POST query

我正在尝试使用 url https://swisstaxcalculator.estv.admin.ch/#/calculator/income-wealth-tax

查询我所在国家/地区的税收计算器

使用浏览器检查器,我知道查询应该如下所示:

{
  "SimKey": null,
  "TaxYear": 2021,
  "TaxLocationID": 100000000,
  "Relationship": 1,
  "Confession1": 5,
  "Children": [
    {
      "Age": 6
    },
    {
      "Age": 11
    }
  ],
  "Confession2": 0,
  "TaxableIncomeCanton": 30000,
  "TaxableIncomeFed": 30000,
  "TaxableFortune": 0
}

我在 R 中使用以下查询,但忽略了 Children 参数。如您所见,我进行了多次尝试,但 none 有效..

url <- https://swisstaxcalculator.estv.admin.ch/delegate/ost-integration/v1/lg-proxy/operation/c3b67379_ESTV/API_calculateSimpleTaxes
  
httr::POST(url, 
           body = list(
             SimKey = NULL,
             TaxYear = 2021,
             TaxLocationID = 100000000,
             Relationship = 1,
             Confession1 = 5,
             # Children = list("Age" = 6, "Age" = 11),
             # Children = array(c(6,11), dimnames = list(c("Age", "Age"))),
             Children ='[{Age:6,Age:11}]',
             Confession2 = 0,
             TaxableIncomeCanton = 30000,
             TaxableIncomeFed = 30000,
             TaxableFortune = 0
           ), 
           encode = "json")

谁能帮我弄清楚如何传递 Children 参数?手动使用计算器我知道这个输入的响应应该如下:

{"response":{"IncomeSimpleTaxCanton":1138,"FortuneTaxCanton":0,"IncomeSimpleTaxCity":1138,"IncomeTaxChurch":0,"IncomeTaxCity":893,"IncomeSimpleTaxFed":0,"PersonalTax":0,"FortuneTaxCity":0,"FortuneSimpleTaxCanton":0,"IncomeTaxFed":0,"FortuneSimpleTaxCity":0,"IncomeTaxCanton":1763,"Location":{"TaxLocationID":100012001,"ZipCode":"1000","BfsID":5586,"CantonID":23,"BfsName":"Lausanne","City":"Chailly-sur-Laus","Canton":"VD"},"FortuneTaxChurch":0}}

您必须从 R:

中将 children 年龄作为嵌套列表传递
url <- "https://swisstaxcalculator.estv.admin.ch/delegate/ost-integration/v1/lg-proxy/operation/c3b67379_ESTV/API_calculateSimpleTaxes"

resp <- httr::POST(url, 
           body = list(
             SimKey = NULL,
             TaxYear = 2021,
             TaxLocationID = 100000000,
             Relationship = 1,
             Confession1 = 5,
             Children = list(list(Age = 6), list(Age = 11)),
             Confession2 = 0,
             TaxableIncomeCanton = 30000,
             TaxableIncomeFed = 30000,
             TaxableFortune = 0
           ), 
           encode = "json")

content <- httr::content(resp, encoding = "UTF-8", as = "text")

jsonlite::fromJSON(content)
#> $response
#> $response$IncomeSimpleTaxCanton
#> [1] 1138
#> 
#> $response$FortuneTaxCanton
#> [1] 0
#> 
#> $response$IncomeSimpleTaxCity
#> [1] 1138
#> 
#> $response$IncomeTaxChurch
#> [1] 0
#> 
#> $response$IncomeTaxCity
#> [1] 893
#> 
#> $response$IncomeSimpleTaxFed
#> [1] 0
#> 
#> $response$PersonalTax
#> [1] 0
#> 
#> $response$FortuneTaxCity
#> [1] 0
#> 
#> $response$FortuneSimpleTaxCanton
#> [1] 0
#> 
#> $response$IncomeTaxFed
#> [1] 0
#> 
#> $response$FortuneSimpleTaxCity
#> [1] 0
#> 
#> $response$IncomeTaxCanton
#> [1] 1763
#> 
#> $response$Location
#> $response$Location$TaxLocationID
#> [1] 100000000
#> 
#> $response$Location$ZipCode
#> [1] "1000"
#> 
#> $response$Location$BfsID
#> [1] 5586
#> 
#> $response$Location$CantonID
#> [1] 23
#> 
#> $response$Location$BfsName
#> [1] "Lausanne"
#> 
#> $response$Location$City
#> [1] "Lausanne"
#> 
#> $response$Location$Canton
#> [1] "VD"
#> 
#> 
#> $response$FortuneTaxChurch
#> [1] 0