在Devise之上构建API的问题

时间:2011-03-02 06:49:29

标签: ruby-on-rails-3 devise

我正在构建一个JSON API,以便能够从移动设备进行通信。

到目前为止,我能够创建一个用户帐户,以获得一个auth_token。但是当我尝试执行PUT请求时,我遇到了一些问题,例如用户的更新操作。这种行为确实很危险,我无法弄清楚出了什么问题。

curl -i -H "Accept: application/json"  'http://localhost:3000/users' -X PUT -d 'auth_token=A830ALUv7ztTdSfl3qxFgi13BJpEf6VzNYe4yk8rEhVxbYYlOc0YMtTGMxkz&user[name]=plop'

返回

HTTP/1.1 401 Unauthorized
Connection: close
Date: Wed, 02 Mar 2011 06:41:29 GMT
Content-Type: application/json; charset=utf-8
Cache-Control: no-cache
X-UA-Compatible: IE=Edge
X-Runtime: 0.157513
Transfer-Encoding: chunked

{"error":"You need to sign in or sign up before continuing."}

在服务器上的日志中我得到了......真的很奇怪。

Started PUT "/users" for 127.0.0.1 at 2011-03-01 22:41:29 -0800
  Processing by Users::RegistrationsController#update as JSON
  Parameters: {"auth_token"=>"A830ALUv7ztTdSfl3qxFgi13BJpEf6VzNYe4yk8rEhVxbYYlOc0YMtTGMxkz", "user"=>{"name"=>"plop"}}
Completed   in 1ms

然而,如果我仅修改从用户到用户表单示例的帖子值的名称,那么请求将起作用

HTTP/1.1 200 OK
Connection: close
Date: Wed, 02 Mar 2011 06:43:30 GMT
Content-Type: application/json; charset=utf-8
ETag: "5e1311709931861e469ce2c8a3b2d0e6"
Cache-Control: max-age=0, private, must-revalidate
X-UA-Compatible: IE=Edge
Set-Cookie: _u_session=BAh7CEkiGXdhcmRlbi51c2VyLnVzZXIua2V5BjoGRVRbCEkiCVVzZXIGOwBGWwZvOhNCU09OOjpPYmplY3RJZAY6CkBkYXRhWxFpUGGaQG%2FaX1pAfhpAaRpAexpAGkAaQ1JIiIkMmEkMTAkMDVBeDczV29UdVFEZHQxZ1JjN0NXdQY7AFRJjW9uRGlzcGF0Y2g6OkZsYXNoOjpGbGFzaEhhc2h7BjoLbm90aWNlSSIrWW91IHVwZGF0ZWQgeW91ciBhY2NvdW50IHN1Y2Nlc3NmdWxseS4GOwBUBjoKQHVzZWRvOghTZXQGOgpAaGFzaHsASSIPc2Vzc2lvbl9pZAY7AEYiJTcyNmIyZjJiODZhOTBiNjUwZDlhZmIwMzA5MTRkMTlm--0e15953fd7edecb0e08255349c4e55f1eddc8d81; path=/; HttpOnly
X-Runtime: 0.162436
Transfer-Encoding: chunked

{"redirect":"/","status":"ok"}

我确切地说,即使没有我的代码(对于API)我也无法执行此请求!!!!

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我不相信Devise处理JSON调用,有一个open issue on github计划在1.3版本中解决此问题。