R httr POST of JSON returns status 400

霸气de小男生 提交于 2019-12-04 18:08:49

When I change dst_get_data to:

dst_get_data <- function(request, table, ..., lang = "en", 
                         format = "CSV", value_presentation = "Default") {

  require(httr)
  require(jsonlite)

  dst_url <- "http://api.statbank.dk/v1/data"

  final_request <- list("table" = table,
                        "lang" = lang,
                        "format" = format,
                        "valuePresentation" = value_presentation)

  data <- POST(url=dst_url, body=final_request, multipart=FALSE)

  return(data)

}

and, the call to it to:

test3 <- dst_get_data(request = test2$basic_request, 
                      table = "folk1", format="JSONSTAT")

I get:

Response [http://api.statbank.dk/v1/data]
  Status: 200
  Content-type: text/json
{"dataset":{"dimension":{"Tid":{"label":"time","category":{"index":{"2014K2":0},"label":{"2014K2":"2014Q2"}}},"id":["Tid"],"size":[1],"role":{"time":["Tid"]}},"label":"Population at the first day of the quarter by time","source":"Statistics Denmark","updated":"2014-05-17T04:10:00Z","value":[5634437],"status":["a"]}} 

I think it was both the need for JSONSTAT and the fact that POST will do the JSON conversion for you automatically.The reason you need JSONSTAT is due to the fact that the "formats" available when you select data from the popup (in the console) are:

<select id="format" name="format"><option value="PX">PX</option>
<option selected="selected" value="CSV">CSV</option>
<option value="XLSX">XLSX</option>
<option value="HTML">HTML</option>
<option value="JSONSTAT">JSONSTAT</option>
<option value="DSTML">DSTML</option>
<option value="PNG">PNG</option>
<option value="BULK">BULK</option>
<option value="AREMOS">AREMOS</option>
<option value="SDMXCOMPACT">SDMXCOMPACT</option>
<option value="SDMXGENERIC">SDMXGENERIC</option>
</select>

Plain ol' JSON is not one of the options.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!