Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/grape/middleware/base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'multi_json'
require 'multi_xml'

module Grape
module Middleware
Expand Down Expand Up @@ -57,7 +58,8 @@ module Formats
:xml => :encode_xml
}
PARSERS = {
:json => :decode_json
:json => :decode_json,
:xml => :decode_xml
}

def formatters
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/grape/middleware/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('<thing><name>Test</name></thing>')})
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
Expand Down