我正在使用Rails gem將請求發送到使用RestClient的api。我需要拯救401錯誤代碼。我看到RESTClient實現文檔如下:需要使用RestClient拯救401狀態代碼
> RestClient.get('http://my-rest-service.com/resource'){ |response,
> request, result, &block| case response.code when 200
> p "It worked !"
> response when 423
> raise SomeCustomExceptionIfYouWant else
> response.return!(request, result, &block) end }
我試圖執行一個類似的案例聲明:
case response.code
when 200
JSON.parse(response.body)
when 401
raise AuthenicationError, "Unauthorized"
else
raise RestClient::ExceptionWithResponse
end
它抓住了200案件罰款,但忽視了401的情況下,並直去的人。對於通過RestClient返回的響應提出401異常的建議?
對,抱歉,我仔細檢查了我的代碼,它實際上是在案件401剛當你輸入這個問題時,你會記得410。 – Bruce1138
你確定響應的代碼實際上是401嗎?在case語句前添加'puts response.code'。 –