httr中的json数组

时间:2018-11-29 15:41:40

标签: r httr jsonlite

我正在尝试使用POST包在httr请求中传递json数组。但是由于数组的格式,我得到了HTTP/1.1 400 Bad Request。这是我建立请求的方式:

decos <- list(name="ais-static-journey")
decos <- jsonlite::toJSON(decos, auto_unbox = TRUE) #Have also tried decos <- toJSON(decos, pretty = TRUE)

body <- list(mmsis=mmsis, startDate= startDate,
             endDate= endDate, decorations = decos)

r <- POST(decoratedurl,body=body, encode="json",add_headers(Authorization=auth), verbose())

我也看过this

我得到的错误主要是因为请求中的反斜杠:

"decorations":"{\"name\":\"ais-static-journey\"}"}

该名称必须为"decorations":[{"name":"ais-static-journey"}] 我该如何实现?

1 个答案:

答案 0 :(得分:0)

想通了。这有效:

decos <- list(list(name="ais-static-journey"))
body <- list(mmsis=mmsis, startDate= startDate,
             endDate= endDate, decorations = decos)
r <- POST(decoratedurl,body=body, encode="json",add_headers(Authorization=auth), verbose())