Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Created August 20, 2013 23:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenharman/6288798 to your computer and use it in GitHub Desktop.
Save stevenharman/6288798 to your computer and use it in GitHub Desktop.
An example of a Request middleware to send the body as XML
module FaradayMiddleware
class XmlRequest < Faraday::Middleware
dependency do
require 'active_support/all'
end
def call(env)
if env[:method] == :post
body = env[:body]
first_key = body.keys.first
env[:body] = body[first_key].to_xml(skip_instruct: true, root: first_key)
end
@app.call(env)
end
end
end
if Faraday.respond_to? :register_middleware
Faraday.register_middleware :request, xml: -> { FaradayMiddlware::XmlRequest }
end
# usage
# Faraday.new(...) do |conn|
# conn.request(:xml)
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment