MultiJson :: LoadError意外令牌

时间:2013-06-15 22:51:59

标签: ruby-on-rails json devise

我正在尝试按照本教程使用Devise创建令牌身份验证:

http://www.strukturedkaos.com/2011/09/19/soup-to-nuts-token-authentication-for-android-using-rails-3-devise/

但是,当我尝试确认教程中概述的sessions_controller工作正常时,我收到错误:

MultiJson::LoadError (795: unexpected token at '{'user':{'username':'test', 'password':'password'}}'):
  json (1.8.0) lib/json/common.rb:155:in `parse'
  json (1.8.0) lib/json/common.rb:155:in `parse'
  multi_json (1.7.7) lib/multi_json/adapters/json_common.rb:16:in `load'
  multi_json (1.7.7) lib/multi_json/adapter.rb:19:in `load'
  multi_json (1.7.7) lib/multi_json.rb:130:in `load'
  activesupport (3.2.8) lib/active_support/json/decoding.rb:15:in `decode'
  actionpack (3.2.8) lib/action_dispatch/middleware/params_parser.rb:47:in `parse_formatted_parameters'
  actionpack (3.2.8) lib/action_dispatch/middleware/params_parser.rb:17:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/flash.rb:242:in `call'
  rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
  rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/cookies.rb:339:in `call'
  activerecord (3.2.8) lib/active_record/query_cache.rb:64:in `call'
  activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `_run__4387220243864305605__call__1717915382536916025__callbacks'
  activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/reloader.rb:65:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.8) lib/rails/engine.rb:479:in `call'
  railties (3.2.8) lib/rails/application.rb:223:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
  thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
  thin (1.5.1) lib/thin/connection.rb:79:in `catch'
  thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
  thin (1.5.1) lib/thin/connection.rb:54:in `process'
  thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
  eventmachine (1.0.3) lib/eventmachine.rb:187:in `run_machine'
  eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
  thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
  thin (1.5.1) lib/thin/server.rb:159:in `start'
  rack (1.4.5) lib/rack/handler/thin.rb:13:in `run'
  rack (1.4.5) lib/rack/server.rb:268:in `start'
  railties (3.2.8) lib/rails/commands/server.rb:70:in `start'
  railties (3.2.8) lib/rails/commands.rb:55:in `block in <top (required)>'
  railties (3.2.8) lib/rails/commands.rb:50:in `tap'
  railties (3.2.8) lib/rails/commands.rb:50:in `<top (required)>'
  script/rails:6:in `require'
  script/rails:6:in `<main>'

这是我测试的命令:

curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000/sessions -d "{'user':{'username':'test', 'password':'password'}}"

我做错了什么?

2 个答案:

答案 0 :(得分:8)

JSON字符串不能用单引号分隔。您必须在键和值周围使用双引号,并使用单引号从shell中转义POST数据。

curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000/sessions -d '{"user":{"username":"test", "password":"password"}}'

答案 1 :(得分:1)

当JSON编写不正确时,通常会发生此错误。我建议使用像http://www.jsoneditoronline.org/

这样的工具检查JSON格式