【发布时间】: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