我正在尝试编写API方法供用户注册狂欢应用。它在我的本地计算机上正常工作,但在服务器上却没有。这是我的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 !@user.save
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":"ml5698@gmail.com","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:ml5698@gmail.com,password:12345678,
JSON::ParserError - 795: unexpected token at '{user:{email:ml5698@gmail.com,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 :(得分:0)
您的错误实际上是在使用命令行卷曲时。您使用-d
开关,并将参数传递给它。参数仅解析到下一个空格,因此传入的参数为
{user:{email:ml5698@gmail.com,password:12345678,
这是您在错误消息中看到的内容,显然JSON不是很好,因此您会收到解析错误。
尝试引用-d
参数,如下所示:
curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST -d '{"user":{"email":"ml5698@gmail.com","password":"12345678","firstname":"M","lastname":"L","mobile":"9999888877"}}' http://localhost:3000/api/users/sign_up