Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Update fiber scheduler to support Ruby 4.1 (#397).
- Improve blocking mechanism for fibers (#401).
- Support `unsubscribe` commands in Action Cable.
- Gracefully handle requests with no content type.

## [1.28.0] - 2026-09-15

Expand Down
6 changes: 3 additions & 3 deletions lib/rage/params_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def self.prepare(env, url_params)
end
end

request_params = if content_type.start_with?("application/json")
request_params = if content_type&.start_with?("application/json")
json_parse(env["rack.input"].tap { |io| io.rewind }.read)
elsif content_type.start_with?("application/x-www-form-urlencoded")
elsif content_type&.start_with?("application/x-www-form-urlencoded") || content_type.nil?
Iodine::Rack::Utils.parse_urlencoded_nested_query(env["rack.input"].tap { |io| io.rewind }.read)
elsif content_type.start_with?("multipart/form-data")
elsif content_type&.start_with?("multipart/form-data")
Iodine::Rack::Utils.parse_multipart(env["rack.input"], content_type)
end

Expand Down
8 changes: 8 additions & 0 deletions lib/rage/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ def route_uri_pattern
end
end

# Get the request body stream.
# @return [IO] the request body stream
# @example
# request.body.read # => '{"name":"Rage"}'
def body
@env["rack.input"].tap(&:rewind)
end

private

def rack_request
Expand Down
9 changes: 9 additions & 0 deletions spec/params_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@
end
end

context "with no content type" do
let(:content_type) { nil }
let(:urlencoded_params) { { slug: "SQjG", parent_ids: %w(4 5), valid: "" } }

it "parses the body as urlencoded params" do
expect(subject).to equal(urlencoded_params)
end
end

context "with malformed urlencoded body" do
let(:urlencoded_params) { { test: true } }

Expand Down
22 changes: 22 additions & 0 deletions spec/rage/request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# frozen_string_literal: true

require "stringio"

RSpec.describe Rage::Request do
let(:env) do
{
Expand Down Expand Up @@ -270,6 +274,24 @@
expect(request.format).to eq("application/json")
end

describe "#body" do
let(:rack_input) { StringIO.new('{"name":"Rage"}') }

before do
env["rack.input"] = rack_input
end

it "returns the Rack request body stream" do
expect(request.body).to equal(rack_input)
end

it "rewinds the body before returning it" do
rack_input.read

expect(request.body.read).to eq('{"name":"Rage"}')
end
end

it "returns the correct request ID" do
expect(request.request_id).to eq("5zfcwiy09bary01l")
end
Expand Down
Loading