【问题标题】:How to make a correct HTTP Post request with Meteor.js to Domino Datalab's Rest API如何使用 Meteor.js 向 Domino Datalab 的 Rest API 发出正确的 HTTP Post 请求
【发布时间】: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 的文档,我尝试了几种不同的方法(同时使用 dataparamsoptions),但这是我最好的尝试:

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 开发人员来说可能很重要。

【问题讨论】:

    标签: rest meteor


    【解决方案1】:

    我是 Domino 的一名工程师,最近致力于 API Endpoints 功能。错误 您收到的消息意味着您发送到我们服务器的 JSON 对象不包含 关键“参数”。我不是 Meteor 方面的专家,但看起来你在使用“参数” 应该在您的 JSON 有效负载中使用“参数”。 9号线左右能换吗……

    {'data': {'params': [143]}}
    

    {'data': {'parameters': [143]}}
    

    如果我对您的代码的理解是正确的,那就可以正常工作。

    干杯!

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      相关资源
      最近更新 更多