Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Sinatra need to rewind the request body?

I made a POST request to a Sinatra app. I noticed that the parameters arrive in the server as a StringIO. It can be read using request.body.read. However, it can only be read once. To read it again, I need to run request.body.rewind (haha, Sinatra).

Why is it designed this way? I can see this being useful in streaming data but are there other applications?

like image 486
Marco Lau Avatar asked Dec 09 '14 05:12

Marco Lau


1 Answers

Parameters are available within Sinatra via the params hash. request.body.read and request.body.rewind are part of Rack, they are not actually implemented within Sinatra. The most common way I have used this in the past is when I'm using Sinatra strictly as a web API and serializing/de-serializing my payload.

like image 114
bigtunacan Avatar answered Sep 28 '22 13:09

bigtunacan