Reputation: 644
I am using IBM Bluemix and the new SPSS Predictive Modelling service. I want to score some data using a model that I deployed in the cloud. In the documentation I have the following Request example:
Content-Type: application/json;charset=UTF-8
Parameters:
Path parameters:
contextId: the identifier of the deployed model to be used to process this score request
Query Parameters:
accesskey: access_key from env.VCAP_SERVICES
Body: the input data, json string, eg.
{
"tablename":"DRUG1n.sav",
"header":["Age", "Sex", "BP", "Cholesterol", "Na", "K", "Drug"],
"data":[[43.0, "M", "LOW", "NORMAL", 0.526102, 0.027164, "drugY"]]
}
I want to do the request using R and the httr package. I wrote the following code:
library(httr)
#Score using a deployed predictive model
host = "https://ibmpmsrvus1.pmservice.ibmcloud.com:8443/pm/v1/score/"
access_key = "vvZ0DIc1d/oAzguseZS/cDf98us5bgi41pau9YEOtu81pRuVE1E2ND6v469pmmerEC2a6an71wxIdhb3gIZ5P7jnBaXJcLpJ+Ta+djR1Uu20nSZ+Rw9rXoOuXOuFsYgUnnipl9lQKr1S2ukXJrA2wA=="
contextId="drug"
url <- paste(host,contextId,"?accesskey=",access_key, sep = "")
data<-toJSON(list("tablename"= unbox("scoreInput"),
"header" = c("Age", "Sex", "BP", "Cholesterol", "Na", "K","Drug"),
"data" = c(unbox(43.0),"M","LOW","NORMAL",0.526102,0.027164,"drugY")
)) r<-POST(url,body = data, encode = 'json') r
The json generated using the toJSON
is a bit different:
{"tablename":"scoreInput","header":["Age","Sex","BP","Cholesterol","Na","K","Drug"],
"data":["43","M","LOW","NORMAL","0.526102","0.027164","drugY"]}
without the [[
at the beginning of data, and that is why it is not working. How can I add this double [?
Any help will be appreciated!
Upvotes: 0
Views: 106
Reputation: 6659
Try adding a content type like this:
r<-POST(url,body = data, content_type_json())
Upvotes: 1