对 API 的 httr POST 请求返回 400 错误 [英] httr POST request to API returns 400 error

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

问题描述

我正在 R 中使用 httr 包以尝试查询 postcode.io API (http://postcodes.io/docs).

I'm working with the httr package in R in an attempt to query the postcode.io API (http://postcodes.io/docs).

我可以使用以下说明成功查询单个邮政编码:sample4 <- GET("api.postcodes.io/postcodes/EN14RF")

I can successfully query a single postcode as per the instructions using: sample4 <- GET("api.postcodes.io/postcodes/EN14RF")

当我尝试查询多个邮政编码时,我变得有点不知所措.postcode.io 说明提示

When I try and query multiple postcodes I'm becoming a little unstuck. The postcode.io instructions suggest

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

其中指定了包含邮政编码数组的 JSON 对象.我有一个包含邮政编码的 R 向量,我尝试将其转换为 JSON 对象:

where a JSON object containing an array of postcodes is specified. I have an R vector containing postcodes which I have attempted to convert to a JSON object as such with:

a <- toJSON(a)

我的 R 向量a"在哪里:

where my R vector 'a' is:

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 时:

Now when I try and query the API with the following line of code:

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

我收到一个错误:提交的 JSON 无效.您需要提交一个包含邮政编码或地理位置对象数组的 JSON 对象"

我有一种感觉,这是因为我没有提供一个数组,而是一个未命名的列表,例如我的 JSON 对象应如下所示:

I have a feeling this is because I am not providing an array but an unnamed list e.g. my JSON object should look like this:

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

不是这个:"[ \"EN14RF\", \"G428QN\", \"E58HB\", \"SA13UL\", \"Bn149aw\", \"CR04BE\" ]"

谁能帮我解决这个问题?我感觉这与我的 toJSON 调用有关,但是在论坛或 API 开发人员页面上找不到类似的示例:(

Can anyone help me with this? I've a feeling it is something to do with my toJSON call, but have been unable to find a similar example on the forum or on the API developers page:(

非常感谢

马蒂

推荐答案

他们的 API 文档写得很差.这似乎是这个特定 API 调用的意图:

They have very poorly written API docs. This seems to be the intent of this particular API call:

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

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

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