ruby Can't Do Web Application Framework

Sinatra request.body.read Can Only Be Read Once Issue

This article introduces the solution to the issue where sinatra request.body.read can only be read once. I don't quite understand it, but <code>request.body.read</code> can get the body of a POST request, but it can only be retrieved once. (Below)

Shou Arisaka
1 min read
Oct 27, 2025

This article introduces the solution to the issue where sinatra request.body.read can only be read once.

I don’t quite understand it, but request.body.read can get the body of a POST request, but it can only be retrieved once. (Below)

require "sinatra"
require "open3"
require 'json'
require 'pry'

post "/androidsms" do

    binding.pry

end
yuis ASUS /mnt/c/pg/ruby_dev$ cat Gemfile.lock | ag sinatra
    sinatra (2.0.5)
  sinatra
[1] pry(#<Sinatra::Application>)> @body = request.body.read
=> "{ \"ContactName\": \"Fumiya\", \"Text\": \"X\", \"OccurredAt\": \"April 21, 2019 at 03:47AM\", \"FromNumber\": \"07044080062\" }"
[2] pry(#<Sinatra::Application>)> request.body.read
=> ""

Image

Hmm, I don’t know if it’s specific behavior to server-side things like Sinatra (or if it’s just a plain bug), Ruby-specific behavior, or something more widespread, but these things happen. I couldn’t find anything like this when I searched, so I have no idea about the cause, but anyway, give me back my 2 hours.

If there’s an object that behaves like this, I’d appreciate it if anyone who knows the name or such could teach me.

Share this article

Shou Arisaka Oct 27, 2025

🔗 Copy Links