rack/rack-contrib — pull request #76 by Kris-LIBIS (2013-09-24 17:45:36+00:00)
Title: Allow post body content custom parsing

I enhanced the Post Body Content parsing wit the option to supply your own custom block to parse the POSTed body with.

For example:

```ruby
use Rack::PostBodyContentTypeParser do |body|
{ payload: JSON.parse(body) }
end
```

This would cause the request body to be loaded into params[:payload] instead of directly into params. There are several use cases why this may be interesting to do:
- Allow the users of your Rack API to supply any JSON data in the body, not just JSON Hashes.
- When combining query parameters with request body, prevent overriding query parameters with body content.

Especially the last part is an important use case for us. We have an API that collects log entries from different sources. Each log entry has a fixed set of parameters that are required and payload that each source can fill in according to its own needs. The fixed parameters are query parameters, the optional payload is request body.

Without this patch the users of the API should take care of always creating a Hash and attach their payload in a 'payload' key of that hash. If they forget, and submit their payload hash as-is the payload is discarded without anybody noticing.

Worse, if the payload hash contains keys that match any of the query parameters, the query parameter values get overwritten as Rack::Request merges both GET and POST hashes without any remorse, causing the entry to be lost in the database as some of the key values contain unexpected values.