How to do a HTTP API call in ruby on rails?

We can do the HTTP API call quite easily and there are multiple ways of doing it. In our example, we are going to do it Net library of the ruby language. When we set up rails this library comes with it.

Following is the code to do an HTTP API call in Ruby on Rails.

How to do a POST call in rails?

uri = URI.parse('Your URL goes here')
http =, uri.port)
http.use_ssl = true

# Set HTTP content type
request =, {'Content-Type' => 'application/json'})

# Set the HTTP post data
data = { email: email, fullName: name }
request.body = data.to_json
response = http.request(request)

# Get response only if it is successful 
response.code == "200" ? response.body : ""

How to do a GET call in rails?

url = URI.parse('Your URL goes here')
req =
res = Net::HTTP.start(, url.port) {|http|
res.code == "200" ? res.body : ""