Reputation: 5476
I'm a beginner Rails programmer and even though this question might be too easy, I love to know if it's possible and if it is how can I accomplish that?
My question inside the index how can I make a get request to a link and assign it's JSON response to an object that I will be later using on. The syntax(not correct) I had in my mind was something like;
people=Make_A_Get_Request("http://people.com") //It will return in JSON
@peopleName=people['name']
I know that it's not true but is there like any method I can apply in Rails like the one above to make a get request to a link and assign its JSON response to an object in my rails function
Upvotes: 0
Views: 332
Reputation: 6728
Try something like below
def index
uri = URI('http://people.com/path/to/request')
response = Net::HTTP.get(uri)
data = JSON.parse(response.body)
#then you can play like data["name"]
rescue Exception => e
logger.info "Unable to do something due to #{e.message}"
end
Upvotes: 1