r - Using RCurl postForm to gather JSON data -
i trying extract time series data bls api using rcurl.
the bls supplies following sample code command line extraction:
curl -i -x post -h 'content-type: application/json' -d '{"seriesid":["leu0254555900", "apu0000701111"], "startyear":"2002", "endyear":"2012"}' http://api.bls.gov/publicapi/v1/timeseries/data/ i have confirmed specified files (i.e. series ids) both present following both yield json formatted object:
require(rcurl) bls.content_test1 <- geturlcontent("http://api.bls.gov/publicapi/v1/timeseries/data/leu0254555900") bls.content_test2 <- geturlcontent("http://api.bls.gov/publicapi/v1/timeseries/data/apu0000701111") based on variety of posts rcurl tag (and post in particular), have ported command line script following chunk of code:
require(rjsonio) jsonbody <- tojson(list("seriesid"=paste('"["cfu0000008000"', '[leu0254555900"]"') ,"startyear"="2012" ,"endyear"="2013")) httpheader <- c(accept="application/json; charset=utf-8", "content-type"="application/json") bls.content <- postform("http://api.bls.gov/publicapi/v1/timeseries/data/" ,.opts=list(httpheader=httpheader ,postfields=jsonbody)) which yields:
[1] "{\"status\":\"request_failed\",\"responsetime\":0,\"message\":[\"your request has failed, please check input parameters , try request again.\"],\"results\":null}" attr(,"content-type") charset "application/json" "utf-8" does appear problem implementation of rcurl or appear problem bls api?
it's problem way creating json body. version if cat(jsonbody) get
{ "seriesid": "\"[\"cfu0000008000\" [leu0254555900\"]\"", "startyear": "2012", "endyear": "2013" } which has escapes , brackets in there. it's not correct. instead try
jsonbody <- tojson(list("seriesid"=c("cfu0000008000", "leu0254555900"), startyear="2012", endyear="2013")) which gives
{ "seriesid": [ "cfu0000008000", "leu0254555900" ], "startyear": "2012", "endyear": "2013" } which valid json. changing part , using rest of code had gives me request_succeeded message.
Comments
Post a Comment