0
我正在练习在轨道中制作API。该API目前只有一个端点通过获取请求接收URL。我的路线是:动态网址在轨道参数
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
namespace :api, defaults: { format: :json } do
namespace :v1 do # resources :orders
get "*link" => "application#parse_link"
end
end
end
我的应用程序控制器代码:
require 'open-uri'
class Api::V1::ApplicationController < ActionController::Base
respond_to :json
# protect_from_forgery with: :exception
def parse_link
begin
url = URI.parse(params[:link])
doc = Nokogiri::HTML(open(url).read)
rescue
redirect_to 'http://localhost:3000/'
end
end
end
当我发送URL是这样的:
http://localhost:3000/api/v1/http://stackoverflow.com/questions/41138538/dynamic-urls-in-rails-params
它工作正常
但以下类型的网址不起作用:
http://localhost:3000/api/v1/http://stackoverflow.com/
在这种情况下,它将该链接,给我这些PARAMS
<ActionController::Parameters {"link"=>"http:/stackoverflow", "format"=>"com"} permitted: true>
正如你可以看到它拆分给定的URL和节省一半的它链接PARAM和“.COM”部分格式参数。
感谢
url-escape参数中的特殊字符:'http:// localhost:3000/api/v1/http%3A%2F%2Fstackoverflow.com%2F'。如果这还不够,也可以避开点。 –
但是编码会在参数上完成.......我得到的参数已经被分割......它不是一个完整的url –
不,这是编码__before__您发送请求。在api客户端。当然不是api服务器。 –