Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 04de14fe86
Fetching contributors…

Cannot retrieve contributors at this time

36 lines (28 sloc) 0.852 kB
require 'multi_json'
module Rack
# A Rack middleware for parsing POST/PUT body data when Content-Type is
# not one of the standard supported types, like <tt>application/json</tt>.
#
# TODO: Find a better name.
#
class PostBodyContentTypeParser
# Constants
#
CONTENT_TYPE = 'CONTENT_TYPE'.freeze
POST_BODY = 'rack.input'.freeze
FORM_INPUT = 'rack.request.form_input'.freeze
FORM_HASH = 'rack.request.form_hash'.freeze
# Supported Content-Types
#
APPLICATION_JSON = 'application/json'.freeze
def initialize(app)
@app = app
end
def call(env)
if Rack::Request.new(env).media_type == APPLICATION_JSON && (body = env[POST_BODY].read).length != 0
env.update(FORM_HASH => MultiJson.load(body), FORM_INPUT => env[POST_BODY])
end
@app.call(env)
end
end
end
Jump to Line
Something went wrong with that request. Please try again.