class Rack::PostBodyContentTypeParser

A Rack middleware for parsing POST/PUT body data when Content-Type is not one of the standard supported types, like application/json.

TODO: Find a better name.

Constants

APPLICATION_JSON

Supported Content-Types

CONTENT_TYPE

Constants

FORM_HASH
FORM_INPUT
POST_BODY

Public Class Methods

new(app) click to toggle source
# File lib/rack/contrib/post_body_content_type_parser.rb, line 27
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/contrib/post_body_content_type_parser.rb, line 31
def call(env)
  if Rack::Request.new(env).media_type == APPLICATION_JSON && (body = env[POST_BODY].read).length != 0
    env[POST_BODY].rewind # somebody might try to read this stream
    env.update(FORM_HASH => JSON.parse(body, :create_additions => false), FORM_INPUT => env[POST_BODY])
  end
  @app.call(env)
end