iOS HTTPS请求到Rails Heroku调试401错误

时间:2017-03-24 20:19:29

标签: ios ruby-on-rails heroku

我正在尝试将this wenderlich tutorial改为Swift 3语法,以创建一个带有在Heroku上托管的rails后端的iOS前端。

让我知道是否有任何其他代码可以分享,但它似乎是一个auth问题。这是为请求设置授权标头的位置:

let basicAuthString = "\(HTTPHelper.API_AUTH_NAME):\(HTTPHelper.API_AUTH_PASSWORD)"
let utf8str = basicAuthString.data(using: String.Encoding.utf8)
let base64EncodedString = utf8str?.base64EncodedString(options: Data.Base64EncodingOptions())

request.addValue("Basic \(base64EncodedString)", forHTTPHeaderField: "Authorization")

我无法告诉如何调试这些请求。如果我单步执行调试器中的代码,请求对象看起来非常平坦,如果我打印请求,我只看到URL。

对heroku服务器的请求(也是从该教程构建的)返回401,它似乎没有进入任何rails方法。它只返回一个未经授权的401:

2017-03-24T20:13:47.022176+00:00 heroku[router]: at=info method=POST path="/api/signin" host=young-retreat-61850.herokuapp.com request_id=9d58e8f4-ee9f-4c73-babd-2a2ee5c82a4f fwd="73.83.200.10" dyno=web.1 connect=1ms service=13ms status=401 bytes=499 protocol=https
2017-03-24T20:13:47.014605+00:00 app[web.1]: Started POST "/api/signin" for 73.83.200.10 at 2017-03-24 20:13:47 +0000
2017-03-24T20:13:47.020239+00:00 app[web.1]: Processing by ApiController#signin as */*
2017-03-24T20:13:47.020279+00:00 app[web.1]:   Parameters: {"email"=>"dsadsadsa", "password"=>"[FILTERED]", "api"=>{"email"=>"dsadsadsa", "password"=>"[FILTERED]"}}
2017-03-24T20:13:47.020671+00:00 app[web.1]: Filter chain halted as #<Proc:0x007f6ae4d6f140@/app/vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.5/lib/action_controller/metal/http_authentication.rb:71> rendered or redirected
2017-03-24T20:13:47.020791+00:00 app[web.1]: Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)
2017-03-24T20:13:47.142925+00:00 app[web.1]: Started POST "/api/signin" for 73.83.200.10 at 2017-03-24 20:13:47 +0000
2017-03-24T20:13:47.148098+00:00 app[web.1]: Processing by ApiController#signin as */*
2017-03-24T20:13:47.148134+00:00 app[web.1]:   Parameters: {"email"=>"dsadsadsa", "password"=>"[FILTERED]", "api"=>{"email"=>"dsadsadsa", "password"=>"[FILTERED]"}}
2017-03-24T20:13:47.156450+00:00 app[web.1]: Filter chain halted as #<Proc:0x007f6ae4d6f140@/app/vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.5/lib/action_controller/metal/http_authentication.rb:71> rendered or redirected
2017-03-24T20:13:47.156647+00:00 app[web.1]: Completed 401 Unauthorized in 8ms (ActiveRecord: 0.0ms)
2017-03-24T20:13:47.157852+00:00 heroku[router]: at=info method=POST path="/api/signin" host=young-retreat-61850.herokuapp.com request_id=bc270aa1-aeed-40b8-b9a0-3c09a68dec8d fwd="73.83.200.10" dyno=web.1 connect=2ms service=24ms status=401 bytes=458 protocol=https

嵌套的“api”键是否具有相同的参数似乎很奇怪?我不清楚调试这些请求的方向。我不能使用像Charles这样的服务,因为它们是https吗?

1 个答案:

答案 0 :(得分:0)

感觉像个傻瓜......

base64EncodedString是可选的。

修复:

request.addValue("Basic \(base64EncodedString!)", forHTTPHeaderField: "Authorization")

我能够通过此URLRequest方法获取有关请求的更多详细信息:

print("request header:\(request.allHTTPHeaderFields)")