2016-08-01 127 views
0

我正在使用R中的httr包嘗試查詢postcode.io API(http://postcodes.io/docs)。httr對API的POST請求返回400錯誤

我可以成功查詢單個郵政編碼按照使用說明: sample4 <- GET("api.postcodes.io/postcodes/EN14RF")

當我嘗試和查詢多個郵政編碼我變得有點粘住。 postcode.io指令建議

POST https://api.postcodes.io/postcodes?q=[postcode] 

其中指定了包含郵編數組的JSON對象。我有一個包含我試圖轉換成JSON對象,例如與郵政編碼的R矢量:

a <- toJSON(a) 

在我的[R向量「a」是:

structure(c(4L, 5L, 3L, 6L, 1L, 2L), .Label = c("Bn14 9aw", "CR0 4BE", "E5 8HB", "EN1 4RF", "G42 8QN", "SA1 3UL"), class = "factor") 

現在,當我嘗試和查詢用下面的代碼行的API:

sample4 <- POST("https://api.postcodes.io/postcodes?q=[postcode]", body = list(postcode = add1JSON)) 

我得到一個錯誤:"Invalid JSON submitted. You need to submit a JSON object with an array of postcodes or geolocation objects"

我有一種感覺,這是因爲我沒有提供一個數組,而是一個未命名的列表,例如我的JSON對象應該是這樣的:

{"postcodes":"[ \"EN14RF\", \"G428QN\", \"E58HB\", \"SA13UL\", \"Bn149aw\", \"CR04BE\" ]"} 

不是這個:"[ \"EN14RF\", \"G428QN\", \"E58HB\", \"SA13UL\", \"Bn149aw\", \"CR04BE\" ]"

誰能幫助我?我感覺它是與我的toJSON電話,但一直無法在論壇或API開發者網頁上:(

非常感謝找到類似的例子

馬蒂

回答

1

。他們有很寫得不好API文檔這似乎是這個特定的API調用的意圖:

library(httr) 
library(jsonlite) 
library(dplyr) 

post_codes <- c("Bn14 9aw", "CR0 4BE", "E5 8HB", "EN1 4RF", "G42 8QN", "SA1 3UL") 

res <- POST("https://api.postcodes.io/postcodes", 
      body=list(postcodes=post_codes), 
      encode="json") 


status_code(res) 
## [1] 200 

content(res, as="text") %>% 
    fromJSON(flatten=TRUE) %>% 
    glimpse() 
## List of 2 
## $ status: int 200 
## $ result:'data.frame': 6 obs. of 29 variables: 
## ..$ query       : chr [1:6] "Bn14 9aw" "E5 8HB" "CR0 4BE" "EN1 4RF" ... 
## ..$ result.postcode     : chr [1:6] "BN14 9AW" "E5 8HB" "CR0 4BE" "EN1 4RF" ... 
## ..$ result.quality     : int [1:6] 1 1 1 1 1 1 
## ..$ result.eastings     : int [1:6] 514948 534934 531978 534957 264583 258092 
## ..$ result.northings     : int [1:6] 104386 185332 164963 199610 192273 662417 
## ..$ result.country     : chr [1:6] "England" "England" "England" "England" ... 
## ..$ result.nhs_ha     : chr [1:6] "South East Coast" "London" "London" "London" ... 
## ..$ result.longitude     : num [1:6] -0.3693 -0.0553 -0.1055 -0.0494 -3.9572 ... 
## ..$ result.latitude     : num [1:6] 50.8 51.6 51.4 51.7 51.6 ... 
## ..$ result.parliamentary_constituency: chr [1:6] "East Worthing and Shoreham" "Hackney South and Shoreditch" "Croydon South" "Enfield North" ... 
## ..$ result.european_electoral_region : chr [1:6] "South East" "London" "London" "London" ... 
## ..$ result.primary_care_trust  : chr [1:6] "West Sussex" "City and Hackney Teaching" "Croydon" "Enfield" ... 
## ..$ result.region     : chr [1:6] "South East" "London" "London" "London" ... 
## ..$ result.lsoa      : chr [1:6] "Worthing 008C" "Hackney 017B" "Croydon 024D" "Enfield 002E" ... 
## ..$ result.msoa      : chr [1:6] "Worthing 008" "Hackney 017" "Croydon 024" "Enfield 002" ... 
## ..$ result.incode     : chr [1:6] "9AW" "8HB" "4BE" "4RF" ... 
## ..$ result.outcode     : chr [1:6] "BN14" "E5" "CR0" "EN1" ... 
## ..$ result.admin_district   : chr [1:6] "Worthing" "Hackney" "Croydon" "Enfield" ... 
## ..$ result.parish     : chr [1:6] "Worthing, unparished area" "Hackney, unparished area" "Croydon, unparished area" "Enfield, unparished area" ... 
## ..$ result.admin_county    : chr [1:6] "West Sussex" NA NA NA ... 
## ..$ result.admin_ward    : chr [1:6] "Gaisford" "Homerton" "Waddon" "Turkey Street" ... 
## ..$ result.ccg      : chr [1:6] NA "NHS City and Hackney" "NHS Croydon" "NHS Enfield" ... 
## ..$ result.nuts      : chr [1:6] "West Sussex (South West)" "Hackney and Newham" "Croydon" "Enfield" ... 
## ..$ result.codes.admin_district  : chr [1:6] "E07000229" "E09000012" "E09000008" "E09000010" ... 
## ..$ result.codes.admin_county  : chr [1:6] "E10000032" "E99999999" "E99999999" "E99999999" ... 
## ..$ result.codes.admin_ward   : chr [1:6] "E05007698" "E05009376" "E05000167" "E05000211" ... 
## ..$ result.codes.parish    : chr [1:6] "E43000150" "E43000202" "E43000198" "E43000200" ... 
## ..$ result.codes.ccg     : chr [1:6] "E38000213" "E38000035" "E38000040" "E38000057" ... 
## ..$ result.codes.nuts    : chr [1:6] "UKJ27" "UKI41" "UKI62" "UKI54" ... 
+1

@hrbmstr感謝了一堆,作品真的很好 –

+0

我對寫得不好的道歉API文檔。我修改了它們 - 特別提到POST請求應該有內容類型的'application/json' –

1

按照HTTR包的documentation,你需要的encode參數設置爲json如下:

url <- "https://api.postcodes.io/postcodes" 
body <- list(a = 1, b = 2, c = 3) 

# JSON encoded 
r <- POST(url, body = body, encode = "json") 

要看到實際的請求,則可以使用verbose()參數從交互式控制檯:

POST(url, body = body, encode = "json", verbose()) 
相關問題