diff --git a/lib/grape/middleware/base.rb b/lib/grape/middleware/base.rb index 5ffb55114..508e73528 100644 --- a/lib/grape/middleware/base.rb +++ b/lib/grape/middleware/base.rb @@ -1,4 +1,5 @@ require 'multi_json' +require 'multi_xml' module Grape module Middleware @@ -57,7 +58,8 @@ module Formats :xml => :encode_xml } PARSERS = { - :json => :decode_json + :json => :decode_json, + :xml => :decode_xml } def formatters @@ -122,6 +124,10 @@ def encode_txt(object) object.respond_to?(:to_txt) ? object.to_txt : object.to_s end + def decode_xml(object) + MultiXml.parse(object) + end + def encode_xml(object) object.respond_to?(:to_xml) ? object.to_xml : object.to_s end diff --git a/spec/grape/middleware/formatter_spec.rb b/spec/grape/middleware/formatter_spec.rb index dafadbcd6..8ddbced8e 100644 --- a/spec/grape/middleware/formatter_spec.rb +++ b/spec/grape/middleware/formatter_spec.rb @@ -141,6 +141,10 @@ def to_xml subject.env['rack.request.form_hash']['is_boolean'].should be_true subject.env['rack.request.form_hash']['string'].should == 'thing' end + it 'should parse the body from an xml POST/PUT and put the contents into rack.request.from_hash' do + subject.call({'PATH_INFO' => '/info.xml', 'Accept' => 'application/xml', 'rack.input' => StringIO.new('Test')}) + subject.env['rack.request.form_hash']['thing']['name'].should == 'Test' + end it 'should be able to fail gracefully if the body is regular POST content' do subject.call({'PATH_INFO' => '/info', 'Accept' => 'application/json', 'rack.input' => StringIO.new('name=Other+Test+Thing')}) subject.env['rack.request.form_hash'].should be_nil