Aug 22
Hi all,
We needed to use net/http of ruby for sending parameters between two modules.
We decided to use the get and the post request of http as follows:
url = URI.parse(“http://SERVER:PORT”)
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Get.new(“/method_name?parameter=a”)
response = http.request(request)
In the above get request the parameter “a” is taken to the “http://SERVER:PORT/method_name” and data related to it is queried.
url = URI.parse(“http://SERVER:PORT”)
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(“/method_name”)
request.set_form_data({“parameter1” => “a”, “parameter2” => “b”, “parameter3” => “c” })
response = http.request(request)
In the above post request three parameters are taken parameter1, parameter2 and parameter3  with the value a,b and c respectively to the “http://SERVER:PORT/method_name” for posting.
When you need to pass a array in the parameters for posting. you can specify it as:
request.set_form_data({“parameter1” => “a”, “parameter2” => [“1”, “2”, “3”], “parameter3” => “c” })
Here the parameter2 takes a array with value 1,2,3.
Now the fun begins,
If you are using ruby 1.9.2 this works fine parameter2 is passed as a array.(parameter2 = [“1”, “2”, “3”])
However if you are uing ruby 1.8.7 this doesnot work, parameter2 appends all the values of the array to form  a string and passes it.(parameter2 = “123”)
You have to override the set_form_data method for it to work and pass parameter as a array.
It can be done as below. You an place this in a file and let the file be included while loading the app.
module Net
module HTTPHeader
def set_form_data(request, params, sep = ‘&’)
request.body = params.map {|k,v|
if v.instance_of?(Array)
v.map {|e| “#{urlencode(k.to_s)}=#{urlencode(e.to_s)}”}.join(sep)
else
“#{urlencode(k.to_s)}=#{urlencode(v.to_s)}”
end
}.join(sep)
request.content_type = ‘application/x-www-form-urlencoded’
end
alias form_data= set_form_data
def urlencode(str)
str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf(‘%%%02x’, s[0]) }
end
end
end
And then use set_form data as follows:
request.set_form_data(request, {“parameter1” => “a”, “parameter2” => [“1”, “2”, “3”], “parameter3” => “c” })
Thus we can pass a array in parameters while use net/http of ruby 1.8.7.
References : http://blog.assimov.net/post/653645115/post-put-arrays-with-ruby-net-http-set-form-data

In one of our projects, we needed to use net/http of ruby for sending parameters between two modules which resided on two different servers.

We decided to use the get and the post request of http as follows:

url = URI.parse("http://SERVER:PORT")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Get.new("/method_name?parameter=a")

response = http.request(request) 

In the above get request the parameter “a” is send to the “http://SERVER:PORT/method_name” and data related to it is queried.

url = URI.parse("http://SERVER:PORT")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Post.new("/method_name")

request.set_form_data({"parameter1" => "a", "parameter2" => "b", "parameter3" => "c" })

response = http.request(request) 

In the above post request three parameters are taken parameter1, parameter2 and parameter3 with the value a,b and c respectively to the “http://SERVER:PORT/method_name” for posting.

When you need to pass a array in the parameters for posting. you can specify it as:

request.set_form_data({"parameter1" => "a", "parameter2" => ["1", "2", "3"], "parameter3" => "c" }) 

Here the parameter2 takes a array with value 1,2,3.

Now the fun begins,

Continue reading »

Tagged with:
Jan 26

I have to admit this . I always fall in love with RAILS each day as I explore it to know how a complex problem can be handle with great ease and with minimum amount of code.

That what made me write this post on " Conditional Get Request."
For those who aren’t aware of it let me give them some insight in it.

Conditional GETs are a feature of the HTTP specification that provide a way for web servers to tell browsers that the response to a GET request hasn’t changed since the last request and can be safely pulled from the browser cache.

They work by using the HTTP_IF_NONE_MATCH and HTTP_IF_MODIFIED_SINCE headers to pass back and forth both a unique content identifier and thetimestamp of when the content was last changed. If the browser makes a request where the content identifier (etag) or last modified since timestamp matches the server’s version then the server only needs to send back an empty response with a not modified status.

Enough of talk now its  time for the action (Way to achieve it in Rails)

class UsersController < ApplicationController
 def show
   @user = User.find(params[:id])
    fresh_when :last_modified => @user.last_updated_at ,:etag => @user
 end
end

That it done

Now let check the logs to find, does it actually asking browser to read from its cache.

First time the page will completely render but any subsequent query for the same page will ask the browser to display from its cache(browser cache) (until modified).

Here my log :-
Conditional GET-Log
Other way to achieve the same is.

 def show
   @user = User.find(param[:id])
    if stale?(:last_modified_at => @user.updated_at,:etag => @user)
      repsond_to do |format|
        format.html
      end
    end
end
 def show
   @user = User.find(params[:id])
    response.last_modified = @user.updated_at
    response.etag = @user
    if request.fresh?(response)
      head :not_modified
    else
      respond_to do |format|
        format.html
      end
    end
 end

The Following Code should give you same result as above

That it , thats all it take to get " Conditional GET " working in Rails. Wasn’t I right when I said that I fall in love with Rails each day.

Usage :-

Now most of you  might be wondering where can they employ this technique or when should they use it

My Answer to them is think of Conditional Get a kind of Caching Mechanism ( after that what browser is doing for you)

Now suppose you have a method in controller that make large number of Database queries or ERB template that render a lot of contents on per requests .Wrapping the same with " Conditional GET " can simply do a lot of good.

That it enough on " Conditional GET " . Hope the post help you in some way or other.

Thank You .

preload preload preload