2015-10-15 85 views
1

我正在嘗試編寫API方法供用戶註冊狂熱應用程序。它在我的本地機器上正常工作,但不在服務器上。這裏是我的user_decorator_controller.rb解析請求參數時發生錯誤JSON :: ParserError - 795:意外令牌

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 

和sign_up.v1.rabl的代碼

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 

當我捲曲服務器請求以下

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 

它給了我下面以上錯誤是從Web服務器提取日誌

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' 

我使用的是Ruby 2.2,rails 4,json 1.8.3,可能是什麼問題,請幫我解決它。

回答

0

你的錯誤實際上是在你使用你的命令行curl。您使用-d開關,並將參數傳遞給它。該參數僅解析,直到下一個空間,所以傳遞的參數是

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

這是你的錯誤信息看什麼,顯然不能很好地形成JSON,讓您得到解析錯誤。

嘗試引用您的-d參數,像這樣:

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 
+0

感謝您的回覆,我可以用這個解決錯誤。 –