Ruby-on-rails Rails:将 API 请求限制为 JSON 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14664049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: Restrict API requests to JSON format
提问by JJD
I would like to restrict requests to all API controllers to being redirected to the JSON path. I would like to use a redirect since also the URL should change according to the response.
One option would be to use a before_filterwhich redirects the request to the same action but forces the JSON format. The example is not working yet!
我想限制对所有 API 控制器的请求被重定向到 JSON 路径。我想使用重定向,因为 URL 也应该根据响应而改变。
一种选择是使用 abefore_filter将请求重定向到相同的操作,但强制使用 JSON 格式。该示例尚未运行!
# base_controller.rb
class Api::V1::BaseController < InheritedResources::Base
before_filter :force_response_format
respond_to :json
def force_response_format
redirect_to, params[:format] = :json
end
end
Another option would be to restrict the format in the routes settings.
另一种选择是限制路由设置中的格式。
# routes.rb
MyApp::Application.routes.draw do
namespace :api, defaults: { format: 'json' } do
namespace :v1 do
resources :posts
end
end
end
I want all request to end up as a JSON request:
我希望所有请求都以 JSON 请求结束:
http://localhost:3000/api/v1/posts
http://localhost:3000/api/v1/posts.html
http://localhost:3000/api/v1/posts.xml
http://localhost:3000/api/v1/posts.json
...
Which strategy would you recommend?
您会推荐哪种策略?
回答by Leo Correa
Setting a default in your routes won't turn all requests into a JSON request.
在路由中设置默认值不会将所有请求转换为 JSON 请求。
What you want is to make sure that whatever you're rendering is a JSON response
您想要的是确保您呈现的任何内容都是 JSON 响应
You pretty much had it in the first option except you need to do this
您几乎在第一个选项中拥有它,除非您需要这样做
before_filter :set_default_response_format
private
def set_default_response_format
request.format = :json
end
That would go under your Base API controller so that when it gets to your actual action the format will always be JSON.
这将在您的 Base API 控制器下进行,以便在您进行实际操作时,格式将始终为 JSON。
回答by PeppyHeppy
If you want to return a 404, or raise a RouteNotFound error if the format is not :json, I would add a route constraint like this:
如果您想返回 404,或者在格式不是 时引发 RouteNotFound 错误:json,我将添加如下路由约束:
Require JSON format:
需要 JSON 格式:
# routes.rb
MyApp::Application.routes.draw do
namespace :api, constraints: { format: 'json' } do
namespace :v1 do
resources :posts
end
end
end
More information can be found here: http://edgeguides.rubyonrails.org/routing.html#request-based-constraints
更多信息可以在这里找到:http: //edgeguides.rubyonrails.org/routing.html#request-based-constraints
回答by mathieugagne
Second option, using routes format. If a user explicitly requests a XML format, he should not receive a JSON response. He should get a message saying that this URL doesn't answer to XML format, or 404.
第二种选择,使用路由格式。如果用户明确请求 XML 格式,他不应收到 JSON 响应。他应该收到一条消息,指出此 URL 不响应 XML 格式或 404。
By the way, it would be rather easy to respond to all which is what you should do in my opinion.
顺便说一下,对我认为你应该做的所有事情做出回应会很容易。
class FooController
respond_to :xml, :json
def show
@bar = Bar.find(params[:id])
respond_with(@bar)
end
end

