Net::HTTP is not as tough as it appears

June 27, 2012

  • Rails
  • Ruby
  • ruby-on-rails
  • api
  • request
  • response
  • net/http

Finally got some time for a new post after a long year gap.

Many of us(Ruby Developers) ignore the Ruby Net::HTTP library when we want to communicate with other external applications from our own app. We mostly tend to use certain gems: restclient, httparty and others, but why not use the a native library which is already packaged and delivered with ruby. There is only one advantage of using the external libraries, it makes your code look shorter, and you are happy with it, ARE YOU?? But wait!!! Think about it. You have included an extra library with a bunch of files in your project, which effects the load time of your application.

So here’s what I did to escape from the usage of external libraries and implement the required functionality in my own way.

require "net/http"
require "active_support"

class HTTPRequest
  def self.send_get_request(request_domain,path,data,headers=nil)
    uri = URI.parse(request_domain)
    http = Net::HTTP.new(uri.host, uri.port)

    if headers == nil
      response = http.send_request('GET',path,data)
    else
      response = http.send_request('GET',path,data,headers)
    end

    response.body
  end

  def self.send_post_request(request_domain,path,data,headers=nil)
    uri = URI.parse(request_domain)
    http = Net::HTTP.new(uri.host, uri.port)

    if headers == nil
      response = http.send_request('POST',path,data)
    else
      response = http.send_request('POST',path,data,headers)
    end

    response.body
  end

  def self.send_put_request(request_domain,path,data,headers=nil)
    uri = URI.parse(request_domain)
    http = Net::HTTP.new(uri.host, uri.port)

    if headers == nil
      response = http.send_request('PUT',path,data)
    else
      response = http.send_request('PUT',path,data,headers)
    end

    response.body
  end

  def self.send_delete_request(request_domain,path,data,headers=nil)
    uri = URI.parse(request_domain)
    http = Net::HTTP.new(uri.host, uri.port)

    if headers == nil
      response = http.send_request('DELETE',path,data)
    else
      response = http.send_request('DELETE',path,data,headers)
    end

    response.body
  end

  def self.send_request(domain, request_type, request_path, data={}, headers={})
    request_path = "/#{request_path}" unless request_path[0] == "/"
    data = data.to_query if data.is_a?(Hash)
    response = {}
    if request_type == "GET"
      response = send_get_request(domain, request_path, data, headers)
    elsif request_type == "POST"
      response = send_post_request(domain, request_path, data, headers)
    elsif request_type == "PUT"
      response = send_put_request(domain, request_path, data, headers)
    elsif request_type == "DELETE"
      response = send_delete_request(domain, request_path, data, headers)
    end

    ActiveSupport::JSON.decode(response)
  end
end

This code helps me a lot, and lets me have a better control over it, which is not possible in the case of external libraries. Hope this piece of code will be helpful to you guys as well.

UPDATE:

I have created a gem using this, you can install it using the command:

gem install http-requestor

The documentation is available here


LOCATION

Mumbai, Maharashtra, India

AROUND THE WEB

Copyright © 2021