r - Using a JSON array in a POST request -
this question has answer here:
- httr post request api returns 400 error 2 answers
i'm writing api wrapper query uk postcodes using httr
package , works fine when use get
requests. i'm lost when comes using post
request.
here's documentation of api says:
accepts json object containing array of postcodes. returns list of matching postcodes , respective available data.
accepts 100 postcodes.
post https://api.postcodes.io/postcodes?q=[postcode]
post data
this method requires json object containing array of postcodes posted. e.g.
{ "postcodes" : ["pr3 0sg", "m45 6gn", "ex165bl"] }
i tried following:
library(httr) pc_json <- '{ "postcodes" : ["pr3 0sg", "m45 6gn", "ex165bl"] }' r <- post(paste0("https://api.postcodes.io/postcodes?q=", pc_json, encode = "json"))
but returns this:
$status 1 400
$error 1 "invalid json submitted. need submit json object array of postcodes or geolocation objects"
the same happens when trim array , use this:
r <- post("https://api.postcodes.io/postcodes?q=ex165bl") content(r)
i read similar threads here , here, didn't make problem easier solve.
any ideas how fix it?
your there need format postcodes list , use body argument of post
encode json
:
library(httr) pc_json <- list( postcodes = c("pr3 0sg", "m45 6gn", "ex165bl") ) res <- post("https://api.postcodes.io/postcodes" , body = pc_json , encode = "json") appdata <- content(res)
Comments
Post a Comment