Open shesko opened 8 years ago
How does your routes.rb
file looks like?
require 'api_constraints'
Rails.application.routes.draw do
devise_for :accounts
namespace :api, defaults: { format: :json },
constraints: { subdomain: 'api' }, path: '/' do
scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
resources :accounts, :only => [:show, :create, :update, :destroy]
resources :sessions, :only => [:create, :destroy]
end
end
end
As you may have noted, I am using accounts instead of users
You endpoints will be under:
api.application.dev/accounts/
Notice that application
is the name of your folder name in case you are using pow or the complete domain.
I know how to access it through pow, however I still don't understand what to use when deployed publicly. Let's assume my domain is www.example.com. What would be the link?
When I try http://api.example.com/accounts/1, the rails server does not receive any request.
I recommend you use naked domains, otherwise
api.example.com/accounts
``
Remember to send the `Accept` header for the correct version. Let me know how it goes
So, to make sure I use the Accept header I decided to use the following curl command:
curl -H 'Accept: application/vnd.app_api.v1' http://api.ww.xx.yyy.zzz:3000/accounts/1
where ww.xx.yyy.zzz is the ip address of my server and the rails app is running on port 3000.
The request does not reach my server. On the other hand if I use the following curl command:
curl -H 'Accept: application/vnd.app_api.v1' http://ww.xx.yyy.zzz:3000/accounts/1
the server receives the requests and returns an html script representing the default rails page. It seems that the "api" part of the full domain name is not recognised by the app.
It probably has to be with the www
try striping that out....
api.xx.zzz:3000/accounts/1
sorry the ww must have confused you. the ww in http://api.ww.xx.yyy.zzz:3000/accounts/1 is meant to be one of the numbers in the ip address of the server
just think it as
curl -H 'Accept: application/vnd.app_api.v1' http://api.xxx.xxx.xxx.xxx:3000/accounts/1
where xxx.xxx.xxx.xxx is the server's ip address
The www
is a subdomain for your domain, that is way the Rails app can't find the endpoint, try the curl without the www
I edited my previous comment. please read above. in the actual address I am not writing the www.
I'm not sure why is that, you get the html response? what response is it?
I get an html error page from my internet provider BT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="refresh" content="0;url=http://www.webaddresshelp.bt.com/main?ParticipantID=mg76cjr54t8kx45jjw4j4k9j5hsr5m26&FailedURI=http%3A%2F%2Fapi.xxx.xxx.xxx%2Faccounts%2F1&FailureMode=1&Implementation=&AddInType=4&Version=pywr1.0&ClientLocation=uk"/>
<script type="text/javascript">url="http://www.webaddresshelp.bt.com/main?ParticipantID=mg76cjr54t8kx45jjw4j4k9j5hsr5m26&FailedURI=http%3A%2F%2Fapi.xxx.xxx.xxx%2Faccounts%2F1&FailureMode=1&Implementation=&AddInType=4&Version=pywr1.0&ClientLocation=uk";if(top.location!=location){var w=window,d=document,e=d.documentElement,b=d.body,x=w.innerWidth||e.clientWidth||b.clientWidth,y=w.innerHeight||e.clientHeight||b.clientHeight;url+="&w="+x+"&h="+y;}window.location.replace(url);</script>
</head>
<body>
</body>
</html>
It is super weird, the port 3000 is open on the server?
Yes it definitely is opened since I get a response from the server when I don't add the api subdomain to the address
Do you have the subdomain configured?, or created on the server?
That must be the problem. I did not know it needed to be set up. As you may have guess, I am a bit of a newbie in all of this. I will let you know if I manage to make it work
thanks for the help !
Hi, First of all thank you very much for this tutorials, I have learnt a lot from them!
However, I have a problem. I am trying to access the api from my iPhone application and I am having problems on figuring out what the correct address is.
When I run the command rails server -b 0.0.0.0 -p 3000, with an IP such as 130.xxx.xxx.xxx, what links should I use to access an account for example?
I tried api.130.xxx.xxx.xxx/accounts/1 but it doesn't seem to reach my rails app.