lib/restfulness/request.rb in restfulness-0.3.1 vs lib/restfulness/request.rb in restfulness-0.3.2
- old
+ new
@@ -54,20 +54,23 @@
def sanitized_query_string
@sanitized_query ||= uri.query ? Sanitizer.sanitize_query_string(uri.query) : ''
end
def params
- return @params if @params || body.nil?
- case headers[:content_type]
- when /application\/json/
- begin
- @params = MultiJson.decode(body)
- rescue MultiJson::LoadError
- raise HTTPException.new(400)
+ @params ||= begin
+ if body.nil? || body.length == 0
+ {}
+ else
+ case headers[:content_type]
+ when /application\/json/
+ @params = params_from_json(body)
+ when /application\/x\-www\-form\-urlencoded/
+ @params = params_from_form(body)
+ else
+ raise HTTPException.new(406)
end
- else
- raise HTTPException.new(406)
+ end
end
end
def sanitized_params
# Note: this returns nil if #params has not been called
@@ -81,9 +84,22 @@
[:get, :post, :put, :patch, :delete, :head, :options].each do |m|
define_method("#{m}?") do
action == m
end
+ end
+
+ protected
+
+ def params_from_json(body)
+ MultiJson.decode(body)
+ rescue MultiJson::LoadError
+ raise HTTPException.new(400)
+ end
+
+ def params_from_form(body)
+ # Sometimes the body can be a StringIO
+ Rack::Utils.parse_query(body.is_a?(StringIO) ? body.read : body)
end
end
end