【发布时间】:2016-05-28 23:53:57
【问题描述】:
在我的服务器端 Meteor.js 方法中,我正在尝试正确地向 Domino Data Lab 的 (DDL) Rest API 发出请求。
DDL 提供了一个平台,可以通过 REST API 调用数据科学模型。他们关于这个 API 的文档在这里:
http://support.dominodatalab.com/hc/en-us/articles/204173149-API-Endpoints-Model-Deployment
但是,我怀疑文档是否有用,因为我认为有经验的 Meteor 开发人员会看到 CURL 或 Python 中的请求示例,并且知道如何将参数正确地转换为 DDL 正在寻找的 JSON 格式。
Domino Datalab 提供了 4 种方法的说明,但不提供 Meteor.js。我将发布 Curl 和 Python 的示例:
示例
CURL 请求
curl -v -X POST \
https://app.dominodatalab.com/MYURL \
-H 'Content-Type: application/json' \
-H 'X-Domino-Api-Key: YOUR_API_KEY' \
-d '{"parameters": [ "FOO", "BAR", "ETC"]}'
Python 请求
import requests
response =
requests.post("https://app.dominodatalab.com/MYURL",
headers = {
"X-Domino-Api-Key": "YOUR_API_KEY",
"Content-Type": "application/json"
},
json = {
"parameters": ["FOO", "BAR", "ETC"]
}
)
print(response.status_code)
print(response.headers)
print(response.json())
根据 Meteor 的文档,我尝试了几种不同的方法(同时使用 data 和 paramsoptions),但这是我最好的尝试:
Meteor.methods({
score_app: function(){
var test = HTTP.call("POST", "https://app.dominodatalab.com/MYURL",
{ headers: {
"Content-Type": "application/json",
"X-Domino-Api-Key": "YOUR_API_KEY"
},
// This is where the problem is. Have tried multiple syntax versions and tried using the `params`options for the HTTP call instead of `data`
data: {'params': [143]
}
},
function (error, result) {
// The syntax below should be if not an error, log the result (for testing etc, otherwise, log "http post error". I may have incorrectly switched this around, but the original version I got from an online example had it the console.log statements in the reverse order.
if (!error) {
console.log(result);
} else{
console.log("http post error");
};
});
}
});
我一直在 Meteor 文档中使用此条目来正确地将参数作为 JSON 对象发送: http://docs.meteor.com/api/http.html
与 Data Domino Lab (DDL) 的连接正确,但无法正确识别参数,因为请求未以 DDL 所需的 JSON 格式发送参数。
result: 'You must provide a JSON object in your request body
with a parameters key containing an array of parameters.' } }
我正在使用 DDL 免费计划,但我会将指向此问题的链接通过电子邮件发送给他们的技术支持。这是一个小众问题,但对于未来希望链接到 DDL 中的数据科学模型的 Meteor.js 开发人员来说可能很重要。
【问题讨论】: