Rails JSON request is not parsed correctly into po

2019-03-08 20:45发布

I'm trying to debug a problem where Rails isn't decoding the JSON POST data.

The server logs show:

2011-12-14T06:44:44+00:00 app[web.2]: Started POST 
2011-12-14T06:44:44+00:00 app[web.2]:   Processing by PostsController#create as */*
2011-12-14T06:44:44+00:00 app[web.2]:   Parameters: {"{\"athlete_id\":\"\",\"known_as\":\"abc\",\"email\":\"abc@defg.com\",\"result\":\"112233\",\"rx\":false,\"mods\":\"thkjth\",\"notes\":\"\"}"=>nil, "affiliate_id"=>"testaffiliate", "wod_id"=>"12345"}

Note that the JSON string is not being parsed - Rails is assigning it as a key in the hash, pointing to a nil value. Does anyone have any ideas before I write a before_filter that tries to JSON.parse all of the params keys?

I don't think this is relevant since I'm sending and receiving data okay, but this problem occurs during a CORS Request from IE (using XDomainRequest).

2条回答
Root(大扎)
2楼-- · 2019-03-08 21:06

You can get past this by setting the Content-Type header to "application/json". You can let the controller know what it expects to return with the Accept header set to "application/json".

The following command with both headers:

curl -d '{ "company": { "name": "acme", "address": "123 Carrot Street" } }' http://0.0.0.0:3000/mysite --header "Accept: application/json" --header "Content-Type: application/json"

Generates this in the logs:

Started POST "/mysite" for 127.0.0.1 at 2012-01-11 16:09:48 -0800
  Processing by MyController#create as JSON
  Parameters: {"company"=>{"name"=>"acme", "address"=>"123 Carrot Street"}, "wassup"=>{"company"=>{"name"=>"acme", "address"=>"123 Carrot Street"}, "controller"=>"wassup", "action"=>"create"}}
Completed 200 OK in 5ms (Views: 2.0ms | ActiveRecord: 0.0ms)

This command with the Accept header:

curl -d '{ "company": { "name": "acme", "address": "123 Carrot Street" } }' http://0.0.0.0:3000/mysite --header "Accept: application/json"

Generates these logs:

Started POST "/mysite" for 127.0.0.1 at 2012-01-11 16:07:26 -0800
  Processing by MyController#create as JSON
  Parameters: {"{ \"company\": { \"name\": \"acme\", \"address\": \"123 Carrot Street\" } }"=>nil}
Completed 200 OK in 7ms (Views: 5.0ms | ActiveRecord: 0.0ms)

And finally this command with the Content-Type header:

curl -d '{ "company": { "name": "acme", "address": "123 Carrot Street" } }' http://0.0.0.0:3000/mysite --header "Content-Type: application/json"

Generates these logs:

Started POST "/mysite" for 127.0.0.1 at 2012-01-11 16:08:11 -0800
  Processing by MyController#create as */*
  Parameters: {"company"=>{"name"=>"acme", "address"=>"123 Carrot Street"}, "wassup"=>{"company"=>{"name"=>"acme", "address"=>"123 Carrot Street"}, "controller"=>"wassup", "action"=>"create"}}
Completed 200 OK in 4ms (Views: 2.0ms | ActiveRecord: 0.0ms)

Notice the parsed parameters and processing message change subtly with each header type.

查看更多
劳资没心,怎么记你
3楼-- · 2019-03-08 21:10
before_filter :fix_ie_params, only: [:create, :update]

For Thin:

def fix_ie_params
  if request.format == '*/*'
    # try to JSON decode in case of IE XDR
    begin

      params.merge! ActiveSupport::JSON.decode(request.body.string)

    rescue Exception=>e
      # todo: log exception
    end
  end
end

For Unicorn and Phusion Passenger:

def fix_ie_params
  if request.format == '*/*'
    # try to JSON decode in case of IE XDR
    begin

      request.body.rewind
      params.merge! ActiveSupport::JSON.decode(request.body.read)

    rescue Exception=>e
      # todo: log exception
    end
  end
end
查看更多
登录 后发表回答