Home > Net >  R/httr pass list to POST query
R/httr pass list to POST query

Time:05-30

I am trying to query the tax calculator for my country using url https://swisstaxcalculator.estv.admin.ch/#/calculator/income-wealth-tax

Using browser inspector I know that the query should look something like this:

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

I'm using the following query in R but the Children argument is ignored. As you can see I have made multiple attempts but none works..

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")

Can anybody help me figuring out how to pass the Children argument? Using the calculator manually I know the response should be as follows for this input:

{"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}}

CodePudding user response:

You have to pass the children ages as a nested list from within R:

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
  • Related