10. Ruby Beyond the Basics II
Activity 10.01: Implementing GET and POST Data to an External Server
Solution:
- We first require the gem, and then need to assign our GET request API to a
url
variable. We then make a GET call usinghttparty
and display the response using an in-built method, parsed_response, which properly parses the JSON response:require 'httparty' url = 'https://www.akshatpaul.com/ruby-fundamentals/list-all-buildings' response = HTTParty.get(url) puts response.parsed_response
- Run this code from the Terminal using the following command:
$ruby get_request.rb
The output should be as follows:
You can implement the same code without a third-party gem dependency by using the in-built
net/http
library and the JSON library. To try this, refer to the code in theget_request_net.rb
file:require 'net/http' require 'json' url = 'https://www.akshatpaul.com/ruby-fundamentals/list-all-buildings...