Get data with api created by curl command

environment

cloud9 (api acquisition side) local (api provider) ruby 2.6.3 Rails 6.0.2.1

Conclusion

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 %>

Error details up to that point

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 ...

Organize again

-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.

important point

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

Get data with api created by curl command
◆ Get API created by Spring Boot from React
Get data of all Premier League players by scraping with Ruby (nokogiri)
Get account information by accessing Strava's API
Generate Minecraft Forge API reference with javadoc command
[Java] Get images with Google Custom Search API
Try to get redmine API key with ruby
Sort by Spring Data JPA (with compound key sort)
Connect nestjs project created by Nx with mysql
I created an api domain with Spring Framework. Part 2
Limit files created with the rails g controller command
[Spring Boot] Get user information with Rest API (beginner)
I tried to get started with Spring Data JPA
I created an api domain with Spring Framework. Part 1