can';t访问grape中间件中的env[';api.endpoint';]
这里使用Grape编写API函数。我想要节流API(API速率限制) lib/grape/extensions/grape_extension.rbcan';t访问grape中间件中的env[';api.endpoint';],api,grape,Api,Grape,这里使用Grape编写API函数。我想要节流API(API速率限制) lib/grape/extensions/grape_extension.rb module Grape module Extension module ThrottleExtension def throttle(options={}) route_setting :throttle, options options end Grape::API
module Grape
module Extension
module ThrottleExtension
def throttle(options={})
route_setting :throttle, options
options
end
Grape::API.extend self
end
end
end
lib/grape/middleware/throttle\u middleware.rb
module Grape
module Middleware
class ThrottleMiddleware < Grape::Middleware::Base
def before
binding.pry
end
end
end
end
最后,在config/application.rb中
require File.expand_path('../../lib/grape_throttle', __FILE__)
config.middleware.use Grape::Middleware::ThrottleMiddleware
当我使用rails s运行并调用api时,binding.pry已经调用了
[1] pry(#<Grape::Middleware::ThrottleMiddleware>)> env['api.endpoint']
=> nil
[1]pry(#)>env['api.endpoint']
=>零
我想知道如何在Grape中间件中访问env['api.endpoint']。您可以通过以下方式访问env::
endpoint=@env[“api.endpoint”]
我知道这已经有几年历史了,但我们今天在生产中遇到了这个问题,这是因为标题中的“内容类型”没有设置为“application/json”。对照您在请求中发送的内容进行检查
我不知道为什么,但我也能够在Grape::API
中注释出我们的用法:
,我立即得到一个“错误”:“请求的内容类型“text/plain”不受支持。”
来自grapes中间件
[1] pry(#<Grape::Middleware::ThrottleMiddleware>)> env['api.endpoint']
=> nil