【问题标题】:Connect to Microsoft Azure Machine Learning Studio Api with ruby instead of python use net/http gem instead of urllib2使用 ruby​​ 而不是 python 连接到 Microsoft Azure Machine Learning Studio Api 使用 net/http gem 而不是 urllib2
【发布时间】:2015-02-19 00:03:00
【问题描述】:

我需要使用 ruby​​ 而不是 python 连接到 Microsoft Azure Machine Learning Studio Api。有人可以帮我使用 net/http gem 将这个 python 代码翻译成 ruby​​。

import urllib2
# If you are using Python 3+, import urllib instead of urllib2

import json 

data =  {
        "Id": "score00001",
        "Instance": {
            "FeatureVector": {
                "value_1"= "1",
                "value_2"= "2",
                "value_3"= "3",
                "value_4"= "4",
                "value_5"= "5",
                "value_6"= "6",
                "value_7"= "7",
                "value_8"= "8",
                "value_9"= "9",
                "value_10"= "10",
            },
            "GlobalParameters": 
                    {
                                            }
        }
    }

body = str.encode(json.dumps(data))

url = 'https://appurl/score'
api_key = 'some_api_key_abc123' # Replace this with the API key for the web service
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}

req = urllib2.Request(url, body, headers) 
response = urllib2.urlopen(req)

# If you are using Python 3+, replace urllib2 with urllib.request in the above code:
# req = urllib.request.Request(url, body, headers) 
# response = urllib.request.urlopen(req)

result = response.read()
print(result)

如何在 ruby​​ 中使用 net/http 发送包含三个字段(url、body、headers)的请求?

【问题讨论】:

  • Read the docs. 但是,对于像这样的复杂 HTTP 请求,我建议使用 a different HTTP library
  • 嗨,Nathan,您从 Azure 收到的 StringofArrays 是您正在寻找的正确输出吗?

标签: python ruby azure urllib net-http


【解决方案1】:

也许Unirest 是完成任务的更好工具,它非常直观。

require 'unirest'

data =  {
        "Id" => "score00001",
        "Instance" => {
            "FeatureVector" => {
                "value_1" => "1",
                "value_2" => "2",
                "value_3" => "3",
                "value_4" => "4",
                "value_5" => "5",
                "value_6" => "6",
                "value_7" => "7",
                "value_8" => "8",
                "value_9" => "9",
                "value_10"=> "10",
            },
            "GlobalParameters" => {}
        }
    }

url = 'https://appurl/score'
api_key = 'some_key'
headers = { 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' + api_key }


response = Unirest.post url, headers: headers, parameters: data

response.code
response.headers
response.body
response.raw_body

【讨论】:

  • 感谢最独特的 gem 工作,我能够从 microsoft azure 获得回复。它返回这个 StringofArrays 对象,我想知道您是否知道除 string.split() 之外的解析它的好方法?任何类似于 ruby​​ 中的 response.read() 的东西:"44441111144441111111111111114444".
  • 看起来像一个xml字符串;如果然后尝试使用Nokogiri gem 呢?在最简单的意义上:在要求nokogiri 之后尝试这个Nokogiri(xml_string)
  • 嗨,Nathan,我也在尝试使用 Azure 创建一个 ruby​​ 应用程序。但遇到了和你一样的问题。它是怎么出来的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-16
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
相关资源
最近更新 更多