2017-02-12 149 views
0

我使用Ruby on Rails構建了一些登錄令牌認證Apis,它在本地運行良好,我有一個內置到本地和heroku數據庫的用戶,如果我這樣做:API調用在本地工作,但不能在Heroku上工作

curl -v -H "Content-Type:application/json" -X POST -d '{"session":{"password":"12345678","email":"[email protected]"}}' http://api.zapserver.dev/sessions/ 

我可以從服務器獲得正確的JSON響應。

但是,當我做的Heroku同一呼叫,這將是這樣的:

curl -k -v -H "Content-Type:application/json" -X POST -d '{"session":{"password":"12345678","email":"[email protected]"}}' https://api.appname.herokuapp.com/sessions/ 

我得到了一個404 Not Found錯誤。

我已經完成了rails db:migrate,我仍然有同樣的錯誤。

任何想法?

編輯

我從Heroku的日誌幾乎一無所有,我用了一個heroku logs --tail命令和什麼都沒有發生。

下面是我從捲曲得到的錯誤消息:

* Trying 50.19.245.201... 
* TCP_NODELAY set 
* Connected to api.zapserver.heroku.com (50.19.245.201) port 443 (#0) 
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 
* Server certificate: *.herokuapp.com 
* Server certificate: DigiCert SHA2 High Assurance Server CA 
* Server certificate: DigiCert High Assurance EV Root CA 
> POST /sessions/ HTTP/1.1 
> Host: api.zapserver.heroku.com 
> User-Agent: curl/7.51.0 
> Accept: */* 
> Content-Type:application/json 
> Content-Length: 67 
> 
* upload completely sent off: 67 out of 67 bytes 
< HTTP/1.1 404 Not Found 
< Connection: keep-alive 
< Server: Cowboy 
< Date: Sun, 12 Feb 2017 14:30:09 GMT 
< Content-Length: 494 
< Content-Type: text/html; charset=utf-8 
< Cache-Control: no-cache, no-store 
< 
<!DOCTYPE html> 
    <html> 
     <head> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <meta charset="utf-8"> 
     <title>No such app</title> 
     <style media="screen"> 
      html,body,iframe { 
      margin: 0; 
      padding: 0; 
      } 
      html,body { 
      height: 100%; 
      overflow: hidden; 
      } 
      iframe { 
      width: 100%; 
      height: 100%; 
      border: 0; 
      } 
     </style> 
     </head> 
     <body> 
     <iframe src="//www.herokucdn.com/error-pages/no-such-app.html"></iframe> 
     </body> 
* Curl_http_done: called premature == 0 
* Connection #0 to host api.zapserver.heroku.com left intact 
+0

你的用戶是否存在於heroku上? –

+0

'rake db:migrate',不是rails。錯字? –

+1

rails db:migrate for rails for rails 5 –

回答

1

所以問題是,如果你想使用類似api.example.com的東西,所以我改變路由有點從api Heroku希望你支付域名.example.com的使用在route.rb xxx.com/api:

namespace :api, defaults: { format: :json }, path: '/api' do 

,而不是代碼在我的老route.rb

namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/' do 

現在,如果我不喜歡這樣的捲曲:

curl -k -v -H "Content-Type:application/json" -X POST -d '{"session":{"password":"12345678","email":"[email protected]"}}' https://example.com/api/sessions/ 

我可以從服務器獲取正確的JSON。

+0

所以你自己解決它。 !大 –

0

404錯誤意味着頁面沒有找到,因此路由錯誤?你可以發佈你的英雄日誌瞭解更多詳情嗎? thx 另外,如下所述。 rails db:migrate不會影響heroku DB。因此你會想運行heroku run rake db:migrate和其他耙子指令

+0

我也這樣做了,仍然沒有。此外,當我執行curl命令時,heroku日誌中沒有任何內容。我在 – DevArenaCN

+0

上面添加了錯誤信息只是想澄清一下,因爲這是一個Rails 5應用程序,那麼你應該使用'''heroku run rails db:migrate'''。 '''rails'''和''rake'''之間沒有更多的切換。儘管如此,這與實際的404問題無關。 – Wes

1

這是域路由工作的方式。當您擁有example.com時,對以該域名結尾的任何地址(例如mail.example.com,www.example.com等)的所有請求都將路由到您配置的DNS服務器。該服務器根據DNS記錄的配置將地址轉換爲IP地址。

您的域的根目錄,在這種情況下,herokuapp.com歸Heroku所有,所以當您創建應用程序時,他們可以爲該應用程序創建一個DNS條目,並將其內部指向您的實例。在Heroku應用程序中運行的應用程序代碼將無法創建額外的子域,而無需對其內部網絡配置提供某種類型的API訪問,這是我無法想象的任何公司都不會允許的機制。所以基本上,這可能永遠不會是一個有效的地址:https://api.appname.herokuapp.com

如果您創建一個自定義域名,當然可以使用您喜歡的任何地址,只要它以您的域名結尾,但您仍然必須配置這些名稱也在你的新DNS主機上。

所以我想說,它與Heroku收取更多費用無關,這只是DNS的工作方式。

相關問題