JSON的httr POST返回状态400 [英] R httr POST of JSON returns status 400

查看:137
本文介绍了JSON的httr POST返回状态400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一些函数,以从 StatBank 丹麦及其 API .他们制作了一个控制台以测试JSON调用,并且我知道我在dst_get_data函数中解析为JSON的basic_request可以工作,因为我已经在控制台中对其进行了测试.

I'm working on a few functions to get data from StatBank Denmark, and their API. They have made a console to test JSON calls and I know the basic_request I parse to JSON in the function dst_get_data works, as I have tested it in the console.

我收到状态400"错误和一条错误消息,提示我应过帐时提供对象".

I get a "status 400" error and an error message that says that I should "supply object when posting".

下面的代码应成为可重现的示例.这是我遇到的第三个函数(dst_get_data).

The code below should make reproducible example. It is the third function (dst_get_data) where I am stuck.

dst_meta <- function(table, ..., lang = "en"){
  require(jsonlite)
  require(httr)

  dkstat_url <- "http://api.statbank.dk/v1/tableinfo"

  params <- list("lang" = lang,
                 "table" = table,
                 "format" = "JSON")

  meta <- POST(url=dkstat_url, body=params, multipart=FALSE)
  meta <- jsonlite::fromJSON(txt=content(meta, as="text"),simplifyDataFrame=TRUE)
  #meta <- RJSONIO::fromJSON(content=content(meta),asText=TRUE, simplify=TRUE, simplifyWithNames=TRUE)

  return(meta)
}

dst_meta_parse <- function(meta, lang){

  basics_names <- c("id", "text", "description", 
                    "unit", "updated", "footnote")

  basics <- meta[names(meta) %in% basics_names]

  variables <- meta[["variables"]][,c("id", "text")]

  values <- meta[["variables"]][,"values"]
  names(values) <- variables$id

  if(lang == "en"){
    test <- grepl(pattern="Tid", names(values))
    if(sum(test) > 0){
      values$Tid$id <- sub(pattern="Q", replacement="K", x=values$Tid$id)
    }
  }

  ## Create basic_request for the data_post file
  basic_request <- vector(mode="list", length=length(variables$id))
  for(variable in 1:length(variables$id)){

    var_name <- variables$id[variable]

    if(var_name == "Tid"){
      basic_request[[variable]] <- list("code" = var_name,
                                       "values" = as.character(values[[var_name]]$id[length(values[[var_name]]$id)]))
    } else {
      basic_request[[variable]] <- list("code" = var_name,
                                       "values" = as.character(values[[var_name]]$id[1]))
    }
  }
  return(list("basics" = basics, "variables" = variables, "values" = values, "basic_request" = basic_request))
}

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,
                        "variables" = request)

  final_request <- jsonlite::toJSON(x=final_request, .escapeEscapes=TRUE, asIs=TRUE)
  print(validate(final_request))
  data <- POST(url=dst_url, body=final_request, multipart=FALSE)

  return(data)
}

test <- dst_meta(table="folk1")

test2 <- dst_meta_parse(meta = test, lang = "en")

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

推荐答案

当我将dst_get_data更改为:

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)

}

,然后调用它:

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

我得到:

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

我认为这既是对JSONSTAT的需要,又是POST会自动为您进行JSON转换的事实.之所以需要JSONSTAT是因为格式"可用当您从弹出式菜单(在控制台中)中选择data时:

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>

简单JSON不是选项之一.

这篇关于JSON的httr POST返回状态400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆