Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line breaks in JSON string lost by Rails controller

Hitting a bit of a brick wall here. I'm trying to send a string containing line breaks (\n, turned to \u000a by JSON.stringify) as part of a JSON object over to a Rails app:

{"bob":{"id":46,"notes":"foo\u000abar\u000abaz"}}

This goes over the wire as this, with \u000a escaped as %5Cu000a:

http://localhost/bobs/46?draft=true&%7B%22bob%22%3A%7B%22id%22%3A46%2C%22notes%22%3A%22foo%5Cu000abar%5Cu000abaz%22%7D%7D=

But the second the request hits Rubyland, the newlines disappear in a puff of ether, turning into spaces:

Processing Api::BobsController#update (for 127.0.0.1 at 2011-05-19 11:01:43) [PUT]
  Parameters: {"draft"=>"true", "action"=>"update", "id"=>"46", "controller"=>"api/bobs", "bob"=>{"notes"=>"foo bar baz", "id"=>46}

And it's not just some logging artifact, but they're going into the database that way as well:

ree-1.8.7-2010.02 > Bob.find_by_id(46)
 => #<Bob id: 46, notes: "foo bar baz"...>

If I send eg. "\\n" instead of "\n", they come through fine:

Processing Api::BobsController#update (for 127.0.0.1 at 2011-05-19 11:01:43) [PUT]
  Parameters: {"draft"=>"true", "action"=>"update", "id"=>"46", "controller"=>"api/bobs", "bob"=>{"notes"=>"foo\\nbar\\nbaz", "id"=>46}

What's going on, and why?

Update: A colleague vaguely recalls hearing that Passenger has been suspected of dropping some special chars, but he can't find a reference to back this up, and neither can I...?

like image 997
lambshaanxy Avatar asked May 19 '11 01:05

lambshaanxy


1 Answers

This could be nothing, but aren't PUT methods meant to be POST'ed in RESTful Rails? GET-ing any URL should be repeatable w/o any change to the database.

If you changed your AJAX call to post you could also indicate proper content-type of application/json so Rails knows how to handle it.

like image 124
Laas Avatar answered Oct 18 '22 13:10

Laas