使用authenticate_or_request_with_http_token时,NoMethodError(未定义的方法`chomp!'代表nil:NilClass)

时间:2014-06-20 10:30:22

标签: ios ruby-on-rails json authentication http-headers

我正在为移动APP创建API

我想用auth_token授权我的请求。我遵循Rails Cast安全API并且使用以下代码

def restrict_access
  authenticate_or_request_with_http_token
  authenticate_or_request_with_http_token do |token, options|
  ApiKey.exists?(access_token: token)
  end
end

当我使用Auth令牌测试curl post请求时,它可以正常工作

curl  -v -H 'Content-Type: application/json' -H 'Accept: application/json'   -X POST http://localhost:3000/users/1/events#create -H 'Authorization: Token token="024cac6efc2a75905ee2aa9cfabae020"' -d  '{ "event":{"address":"Leo Technosoft, Magarpatta", "description":"Gettogether of employees", "name":"iPhone event"}}'

但是如果从IOS APP获得请求,我会收到类似

的错误
NoMethodError (undefined method `chomp!' for nil:NilClass):

来自IOS的请求看起来像

{
Accept = "application/json";
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
"Authorization:Token token" = 024cac6efc2a75905ee2aa9cfabae020;
"Content-Type" = "application/json";
"User-Agent" = "Quickvent/1.0 (iPhone Simulator; iOS 7.0; Scale/2.00)";
}

与我的卷曲请求相同

 POST /users/1/events HTTP/1.1
 > User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
 > Host: localhost:3000
 > Content-Type: application/json
 > Accept: application/json
  > Authorization: Token token="024cac6efc2a75905ee2aa9cfabae020"
 > Content-Length: 116
  > 
 * upload completely sent off: 116out of 116 bytes
 < HTTP/1.1 201 Created 
 < Location: http://localhost:3000/users/1/events/6 

我无法理解问题所在。

由于

0 个答案:

没有答案