Class: FaradayMiddleware::MethodOverride
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- FaradayMiddleware::MethodOverride
- Defined in:
- lib/faraday_middleware/request/method_override.rb
Overview
Public: Writes the original HTTP method to “X-Http-Method-Override” header and sends the request as POST.
This can be used to work around technical issues with making non-POST requests, e.g. faulty HTTP client or server router.
This header is recognized in Rack apps by default, courtesy of the Rack::MethodOverride module. See rack.rubyforge.org/doc/classes/Rack/MethodOverride.html
Constant Summary collapse
- HEADER =
'X-Http-Method-Override'
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = nil) ⇒ MethodOverride
constructor
Public: Initialize the middleware.
-
#rewrite_request(env, original_method) ⇒ Object
Internal: Write the original HTTP method to header, change method to POST.
- #rewrite_request?(method) ⇒ Boolean
Constructor Details
#initialize(app, options = nil) ⇒ MethodOverride
Public: Initialize the middleware.
app - the Faraday app to wrap options - (optional)
:rewrite - Array of HTTP methods to rewrite
(default: all but GET and POST)
24 25 26 27 28 29 30 |
# File 'lib/faraday_middleware/request/method_override.rb', line 24 def initialize(app, = nil) super(app) @methods = &.fetch(:rewrite)&.map do |method| method = method.downcase if method.respond_to? :downcase method.to_sym end end |
Instance Method Details
#call(env) ⇒ Object
32 33 34 35 36 |
# File 'lib/faraday_middleware/request/method_override.rb', line 32 def call(env) method = env[:method] rewrite_request(env, method) if rewrite_request?(method) @app.call(env) end |
#rewrite_request(env, original_method) ⇒ Object
Internal: Write the original HTTP method to header, change method to POST.
47 48 49 50 |
# File 'lib/faraday_middleware/request/method_override.rb', line 47 def rewrite_request(env, original_method) env[:request_headers][HEADER] = original_method.to_s.upcase env[:method] = :post end |
#rewrite_request?(method) ⇒ Boolean
38 39 40 41 42 43 44 |
# File 'lib/faraday_middleware/request/method_override.rb', line 38 def rewrite_request?(method) if @methods.nil? || @methods.empty? (method != :get) && (method != :post) else @methods.include? method end end |