rails curl 转换为 Net::HTTP 时出错
rails error on curl converted to Net::HTTP
我在尝试 运行 将 curl 命令转换为 Rails 中的 Net::HTTP 时收到以下错误。 url+params 在客户端使用 AJAX 运行 时给了我预期的响应,但当然这会暴露我的 API 密钥。
知道为什么会出现此错误吗?
错误:
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代码:
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
看来您使用的是 ruby v1.9.3,所以我按以下方式重写了您的代码:
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
我在尝试 运行 将 curl 命令转换为 Rails 中的 Net::HTTP 时收到以下错误。 url+params 在客户端使用 AJAX 运行 时给了我预期的响应,但当然这会暴露我的 API 密钥。
知道为什么会出现此错误吗?
错误:
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代码:
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
看来您使用的是 ruby v1.9.3,所以我按以下方式重写了您的代码:
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