Marco Lau
Marco Lau

Reputation: 617

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?

Upvotes: 15

Views: 5237

Answers (1)

bigtunacan
bigtunacan

Reputation: 4996

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.

Upvotes: 8

Related Questions