2017-03-24 39 views
0

我試圖修改this wenderlich tutorial到Swift 3語法來創建一個在Heroku上託管的rails backend的iOS版本。iOS HTTPS請求Rails Heroku調試401錯誤

讓我知道是否有任何其他代碼可以很好地共享,但它似乎是一個驗證問題。在此處,授權頭被設置爲請求:

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:[email protected]/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:[email protected]/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」鍵有相同的參數看起來很奇怪嗎?我不清楚調試這些請求需要採取哪個方向。我不能使用像查爾斯這樣的服務,因爲他們的https是正確的?

回答

0

感覺像一個傻瓜...

base64EncodedString是一個可選項。

修復:

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

我能得到有關與此的URLRequest方法要求更多的細節:

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