Search code examples
ruby-on-railsjsoncurlheroku

API calls work locally, but not working on Heroku


I have build some login token auth Apis using Ruby on Rails, it works well on local, I have a user built into the local and heroku database, and if I do this :

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

I can get the correct JSON response from the server.

But, when I do the same call to Heroku, which would be something like this:

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

I got a 404 Not Found error.

I have done the rails db:migrate and I still got the same error.

Any ideas?

EDIT

I got literally nothing from Heroku log, I used a heroku logs --tail command and nothing happened.

Here's the error message I got from the Curl:

*   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

Solution

  • So the problem is that Heroku wants you to pay for the domain if you wanna use something like api.example.com, so I change the routing a bit from api.example.com to xxx.com/api in route.rb using:

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

    instead of the code in my old route.rb

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

    And now if I do the Curl like this:

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

    I can get the correct JSON back from the server.