Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does request.body require rewind before read with Rails 4 and content-type application/json or is there a new approach?

When I post data to a Rails 4 application with Content-Type: "application/x-www-form-urlencoded" I can get the body of the request with request.body.read

However, when I post data with Content-Type "application/json", request.body.read is blank unless I call request.body.rewind first.

Two questions:

1.) How do I get the raw request body without calling rewind on the stream first?

2.) Is it possible to stop Rails (or Rack) parsing the JSON in the request body when the client sends content-type application/json

Note that in Rails 3, like Rails 4 the parsed JSON is available in params, but request.body.read does not require rewind first.

like image 460
Cameron Walsh Avatar asked May 17 '13 01:05

Cameron Walsh


2 Answers

This is indeed a bug. The fix has been integrated into master and should presumably pop up in 4.0.1.

like image 114
colinm Avatar answered Oct 16 '22 02:10

colinm


Use request.env instead of request.body.read.

like image 2
Adrien Lamothe Avatar answered Oct 16 '22 02:10

Adrien Lamothe