0

I receive the following error when trying to run a curl command converted to Net::HTTP in Rails. The url+params gives me the expected response when I run it on the client side using AJAX but of course this exposes my API key.

Any idea why this error is occurring?

error:

undefined method `empty?' for #<URI::HTTPS:0x00000006552fe8>

curl:

curl -X POST -d "maxRetrieve=1" -d "outputMode=json" -d "url=https://www.whitehouse.gov/the-press-office/2016/03/19/weekly-address-president-obamas-supreme-court-nomination" "https://gateway-a.watsonplatform.net/calls/url/URLGetRelations?apikey=ABC123"

rails code:

require 'net/http'
require 'uri'
require 'json'

url = "https://gateway-a.watsonplatform.net/calls/url/URLGetRelations?apikey=ABC123"
encoded_url = URI.encode(url)
uri = URI.parse(encoded_url)
request = Net::HTTP::Post.new(uri)
request.set_form_data(
  "maxRetrieve" => "1",
  "outputMode" => "json",
  "url" => "https://www.whitehouse.gov/the-press-office/2016/03/19/weekly-address-president-obamas-supreme-court-nomination",
)

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
  http.request(request)
end
puts response
JeffA
  • 166
  • 1
  • 17

1 Answers1

1

Looks like you're using the ruby v1.9.3 so I rewrote your code in the following way:

uri = URI.parse("https://gateway-a.watsonplatform.net/calls/url/URLGetRelations?apikey=ABC123")
args = {
      "maxRetrieve" => "1",
      "outputMode" => "json",
      "url" => "https://www.whitehouse.gov/the-press-office/2016/03/19/weekly-address-president-obamas-supreme-court-nomination",
}

uri.query = URI.encode_www_form(args)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

request = Net::HTTP::Get.new(uri.request_uri)

response = http.request(request)
puts response.body
  • Thanks. That was a typo on my part. Regardless if I use:"request = Net::HTTP::Get.new(uri)" or " request = Net::HTTP::Post.new(uri)" I receive the same error. – JeffA Dec 12 '16 at 22:30
  • Thanks. You definitely pointed me in the right direction. Please note, that I had to move the apikey to the args to actually get it to work. – JeffA Dec 12 '16 at 23:18