Skip to content

Commit 5f96caf

Browse files
committed
Merge pull request ruby-grape#62 from jwillis/master
Ability to handle incoming XML in the body
2 parents c406b1b + 77dd0b3 commit 5f96caf

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/grape/middleware/base.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'multi_json'
2+
require 'multi_xml'
23

34
module Grape
45
module Middleware
@@ -57,7 +58,8 @@ module Formats
5758
:xml => :encode_xml
5859
}
5960
PARSERS = {
60-
:json => :decode_json
61+
:json => :decode_json,
62+
:xml => :decode_xml
6163
}
6264

6365
def formatters
@@ -122,6 +124,10 @@ def encode_txt(object)
122124
object.respond_to?(:to_txt) ? object.to_txt : object.to_s
123125
end
124126

127+
def decode_xml(object)
128+
MultiXml.parse(object)
129+
end
130+
125131
def encode_xml(object)
126132
object.respond_to?(:to_xml) ? object.to_xml : object.to_s
127133
end

spec/grape/middleware/formatter_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ def to_xml
141141
subject.env['rack.request.form_hash']['is_boolean'].should be_true
142142
subject.env['rack.request.form_hash']['string'].should == 'thing'
143143
end
144+
it 'should parse the body from an xml POST/PUT and put the contents into rack.request.from_hash' do
145+
subject.call({'PATH_INFO' => '/info.xml', 'Accept' => 'application/xml', 'rack.input' => StringIO.new('<thing><name>Test</name></thing>')})
146+
subject.env['rack.request.form_hash']['thing']['name'].should == 'Test'
147+
end
144148
it 'should be able to fail gracefully if the body is regular POST content' do
145149
subject.call({'PATH_INFO' => '/info', 'Accept' => 'application/json', 'rack.input' => StringIO.new('name=Other+Test+Thing')})
146150
subject.env['rack.request.form_hash'].should be_nil

0 commit comments

Comments
 (0)