MrWater
MrWater

Reputation: 1797

Params variable not getting filled with JSON post data....Why?

I had this service working before, with some other fields, but right now i seem not to get it working, and am not quite getting why.

I'm posting the following json to my Rails app (with RestClient 2.4, for the test) :

{
    "camp":{
        "total": 100,
        "number": "epw1f6e"
    },
    "machine":{
        "serial_number": "1234567",
        "token": "fONxDN"
    }
}

Camp controller :

def create
    if is_it_json?
      logger.debug params.inspect
      machine = Machine.find_by_serial_number(params[:machine][:serial_number])
...

From the logger, I get that params are : {"action"=>"create", "controller"=>"camp"}

Where is the json information??

My log error :

Started POST "/camps" for 127.0.0.1 at 2012-07-15 00:08:21 +0100
Processing by CampsController#create as JSON
WARNING: Can't verify CSRF token authenticity
{"action"=>"create", "controller"=>"camps"}
Completed 500 Internal Server Error in 2ms

NoMethodError (undefined method `[]' for nil:NilClass):
  app/controllers/camps_controller.rb:24:in `create'

What might i be doing wrong?

Thank you

Upvotes: 2

Views: 1433

Answers (2)

MrWater
MrWater

Reputation: 1797

After a lot of reading and questioning, i just found out the solution :

The params weren't getting filled because i was missing Headers (I had the Accept, but was missing the Content-type) :

Accept: application/json
Content-type: application/json

Adding the headers solved this issue.

Upvotes: 6

hank8903
hank8903

Reputation: 11

Can't verify CSRF token authenticity {"action"=>"create", "controller"=>"camps"}
Completed 500 Internal Server Error in 2ms

That error is thrown when an invalid (or nonexistent) CSRF token is posted with your json. The CSRF needs to be attached to the header of any posted data. For testing you can disable CSRF tokens by adding the following to your controller (for Rails 3):

protect_from_forgery :except => :create

Do not leave that in your controller when the site goes into production. CSRF tokens prevent CSRF attacks. Refer to the Ruby on Rails Security Guide for more details.

Let me know if that does the trick!

Upvotes: 0

Related Questions