Skip to content

Instantly share code, notes, and snippets.

@jodosha
Created November 18, 2022 14:03
Show Gist options
  • Save jodosha/27b53ddadc7218b2bd6e0a9490045793 to your computer and use it in GitHub Desktop.
Save jodosha/27b53ddadc7218b2bd6e0a9490045793 to your computer and use it in GitHub Desktop.
Rack upload exception
{"foo": "100% bar"}

Steps

From one shell:

bundle install
bundle exec puma

From another shell:

⚡ curl -X POST http://localhost:9292/ \
   --header 'Content-Type: multipart/form-data; boundary=---------AaB03x' \
   --data-binary @./1.json

Puma caught this error: Rack::Multipart::EmptyContentError (Rack::Multipart::EmptyContentError)
/Users/jodosha/.gem/ruby/3.1.0/gems/rack-3.0.0/lib/rack/multipart/parser.rb:417:in `handle_empty_content!'
/Users/jodosha/.gem/ruby/3.1.0/gems/rack-3.0.0/lib/rack/multipart/parser.rb:251:in `read_data'
/Users/jodosha/.gem/ruby/3.1.0/gems/rack-3.0.0/lib/rack/multipart/parser.rb:227:in `block in parse'
/Users/jodosha/.gem/ruby/3.1.0/gems/rack-3.0.0/lib/rack/multipart/parser.rb:212:in `loop'
/Users/jodosha/.gem/ruby/3.1.0/gems/rack-3.0.0/lib/rack/multipart/parser.rb:212:in `parse'
/Users/jodosha/.gem/ruby/3.1.0/gems/rack-3.0.0/lib/rack/multipart/parser.rb:105:in `parse'
/Users/jodosha/.gem/ruby/3.1.0/gems/rack-3.0.0/lib/rack/multipart.rb:29:in `parse_multipart'
config.ru:12:in `call'
/Users/jodosha/.gem/ruby/3.1.0/gems/puma-6.0.0/lib/puma/configuration.rb:268:in `call'
/Users/jodosha/.gem/ruby/3.1.0/gems/puma-6.0.0/lib/puma/request.rb:89:in `block in handle_request'
/Users/jodosha/.gem/ruby/3.1.0/gems/puma-6.0.0/lib/puma/thread_pool.rb:343:in `with_force_shutdown'
/Users/jodosha/.gem/ruby/3.1.0/gems/puma-6.0.0/lib/puma/request.rb:88:in `handle_request'
/Users/jodosha/.gem/ruby/3.1.0/gems/puma-6.0.0/lib/puma/server.rb:430:in `process_client'
/Users/jodosha/.gem/ruby/3.1.0/gems/puma-6.0.0/lib/puma/server.rb:233:in `block in run'
/Users/jodosha/.gem/ruby/3.1.0/gems/puma-6.0.0/lib/puma/thread_pool.rb:150:in `block in spawn_thread'%
# frozen_string_literal: true
require "rack/builder"
require "rack/multipart"
class FormParser
def initialize(app)
@app = app
end
def call(env)
env["form.params"] = Rack::Multipart.parse_multipart(env)
@app.call(env)
end
end
class App
def call(env)
params = env["form.params"]
body = params.inspect
[200, {"Content-Length" => body.bytesize}, [body]]
end
end
use FormParser
run App.new
# frozen_string_literal: true
source "https://rubygems.org"
gem "rack"
gem "puma"
GEM
remote: https://rubygems.org/
specs:
nio4r (2.5.8)
puma (6.0.0)
nio4r (~> 2.0)
rack (3.0.0)
PLATFORMS
x86_64-darwin-21
DEPENDENCIES
puma
rack
BUNDLED WITH
2.3.25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment