Skip to content

Add raw_body to ParamParser for multi-handler body access#740

Merged
sdogruyol merged 1 commit intomasterfrom
raw-body
Mar 3, 2026
Merged

Add raw_body to ParamParser for multi-handler body access#740
sdogruyol merged 1 commit intomasterfrom
raw-body

Conversation

@sdogruyol
Copy link
Member

@sdogruyol sdogruyol commented Feb 24, 2026

Closes #732

Problem

Currently request body IO can only be consumed once. When multiple handlers need the body (e.g. kemal-session for CSRF, kemal-controller for params), the second handler gets an empty body.

Solution

Add a public raw_body method to ParamParser that reads and caches the body on first access. Both parse_body and parse_json now use this cached value, so multiple handlers can access the body via env.params.raw_body without consuming the IO.

Changes

  • Add raw_body to ParamParser (caches for application/x-www-form-urlencoded and application/json)
  • Update parse_body and parse_json to use raw_body instead of reading directly from @request.body
  • Multipart requests unchanged; parse_files still streams via HTTP::FormData.parse

Usage

post "/" do |env|
  raw = env.params.raw_body  # raw body, multiple handlers can call it
  env.params.body["name"]    # parsed body
end

@sdogruyol sdogruyol merged commit f618eec into master Mar 3, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Be able to access request body in more than one handler

1 participant