0

I need to connect to the Microsoft Azure Machine Learning Studio Api with ruby instead of python. Can someone help me translate this python code into ruby using the net/http gem.

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)

How do I sent a request with net/http with three fields (url, body, headers) in ruby?

  • [Read the docs.](http://ruby-doc.org/stdlib-2.1.5/libdoc/net/http/rdoc/Net/HTTP.html) However, I would recommend using [a different HTTP library](https://www.ruby-toolbox.com/categories/http_clients) for complicated HTTP requests like this. – Adrian Dec 20 '14 at 03:25
  • Hi Nathan, was the StringofArrays you received from Azure the correct output you were looking for? – Tyrion Lannister May 19 '15 at 02:15

1 Answers1

1

Perhaps Unirest is a better tool for the task, its very intuitive.

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
Jikku Jose
  • 18,306
  • 11
  • 41
  • 61
  • Thanks the unirest gem work and I was able to get a response back from microsoft azure. It returns this StringofArrays object and I was wondering if you knew a good way to parse it other than string.split()? Anything like response.read() in ruby: "44441111144441111111111111114444". – Nathan Wadhwani Dec 20 '14 at 20:26
  • Seems like an xml string; if then try using `Nokogiri` gem for it? In simplest sense: try this `Nokogiri(xml_string)` after requiring `nokogiri`. – Jikku Jose Dec 21 '14 at 03:54
  • Hi Nathan, I was trying to create a ruby application with Azure as well. but ran into the same problem as you. how did it come out? – Tyrion Lannister May 13 '15 at 02:50