cloud9 (api acquisition side) local (api provider) ruby 2.6.3 Rails 6.0.2.1
controller
def index
uri = `curl -v -X GET "http://stage-smartyoyaku-env.ap-northeast- 1.elasticbeanstalk.com/api/v1/tasks" \
-H "Authorization:Bearer Write Token here"`
@test = JSON.parse(uri)
end
viewfile
<%= @test %>
When you enter the curl command in the cloud9 terminal. ..
curl: (7) Failed to connect to localhost port 3000: Connection refused
When I look it up, it seems that the server is not started or the port number is wrong. I accessed localhost: 3000 with a browser, but there is no problem. There was also a port number, but ...
-I'm hitting the curl command on the cloud9 side app. (Acquiring side) -Launched an application that provides api on 3000 port in local environment. (Providing side) ← Important here
Hmm? You can't connect to local from cloud9 (on the internet),
$ lsof -i:3000
$
Check if 3000port is used on cloud9 → Not displayed
In other words, the side that provides the api needs to publish the url on the net with heroku etc. and fetch the data from it.
There is a slight difference in writing when typing the curl command in the terminal and when writing in the controller. In controller, enclose the curl command in ``.
For terminal
$ curl -v -X GET "http://stage-smartyoyaku-env.ap-northeast-1.elasticbeanstalk.com/api/v1/tasks" \
-H "Authorization:Bearer Write Token here"
For controller
def index
uri = `curl -v -X GET "http://stage-smartyoyaku-env.ap-northeast- 1.elasticbeanstalk.com/api/v1/tasks" \
-H "Authorization:Bearer Write Token here"`
end
Recommended Posts