Search code examples
ruby-on-railsjsonapispreespree-auth-devise

Error occurred while parsing request parameters JSON::ParserError - 795: unexpected token at


I am trying to write the API methods for user to sign up on spree app. It's working properly on my local machine but not on server. here is my code of user_decorator_controller.rb

def sign_up
        @user = Spree::User.find_by_email(params[:user][:email])    
        if @user.present?
          render "spree/api/users/user_exists", :status => 401 and return
        end
        @user = Spree::User.new(user_params)
        if [email protected]
          unauthorized
          return
        end
        @user.generate_spree_api_key!
      end

and sign_up.v1.rabl is

object @user
attributes :id, :spree_api_key, :email, :firstname, :lastname, :mobile

child(:bill_address => :bill_address) do
  extends "spree/api/addresses/show"
end

child(:ship_address => :ship_address) do
  extends "spree/api/addresses/show"
end

when I CURL the server with below request

 curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST -d {"user":{"email":"[email protected]","password":"12345678", "firstname":"M", "lastname":"L", "mobile":"9999888877"}} http://localhost:3000/api/users/sign_up

It gives me above error below is extracts from web server log

Started POST "/api/users/sign_up" for 127.0.0.1 at 2015-10-15 11:23:36 +0530
  ActiveRecord::SchemaMigration Load (0.3ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
Error occurred while parsing request parameters.
Contents:

{user:{email:[email protected],password:12345678,

JSON::ParserError - 795: unexpected token at '{user:{email:[email protected],password:12345678,':
  json (1.8.3) lib/json/common.rb:155:in `parse'
  activesupport (4.2.4) lib/active_support/json/decoding.rb:26:in `decode'

I am using Ruby 2.2 , rails 4, json 1.8.3 , what could be the issue, please help me resolve it.


Solution

  • Your error is actually in your use of your command line curl. You use the -d switch, and pass parameters to it. The parameters are only parsed until the next space, so your parameters being passed in are

    {user:{email:[email protected],password:12345678,
    

    This is what you're seeing in the error message, and is obviously not well formed JSON, so you get the parsing error.

    Try quoting your -d parameters like so:

    curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST -d '{"user":{"email":"[email protected]","password":"12345678","firstname":"M","lastname":"L","mobile":"9999888877"}}' http://localhost:3000/api/users/sign_up