New to ruby and trying to do a HTTP POST. From looking at documentation
it states
“In version 1.1 (ruby 1.6), this method returns a pair of objects, a
Net::HTTPResponse object and an entity body string. In version 1.2 (ruby
1.8), this method returns a Net::HTTPResponse object.”
What does it mean by version 1.1 and 1.2?
I am using ruby version 1.9.2 and the method is returning a pair of
objects:
Net::HTTPResponse
string
I am under the impression I am using version 1.2 of the POST and
therefore should be recieving an object(Net::HTTPResponse)
New to ruby and trying to do a HTTP POST. From looking at documentation
it states
“In version 1.1 (ruby 1.6), this method returns a pair of objects, a
Net::HTTPResponse object and an entity body string. In version 1.2 (ruby
1.8), this method returns a Net::HTTPResponse object.”
What does it mean by version 1.1 and 1.2?
Of the Net::HTTP API. Look in the source; depending on your system this
may be somewhere like /usr/lib/ruby/1.8/net/http.rb
== Switching Net::HTTP versions
You can use net/http.rb 1.1 features (bundled with Ruby 1.6)
by calling HTTP.version_1_1. Calling Net::HTTP.version_1_2
allows you to use 1.2 features again.
# example
Net::HTTP.start {|http1| …(http1 has 1.2 features)… }
Net::HTTP.version_1_1
Net::HTTP.start {|http2| …(http2 has 1.1 features)… }
Net::HTTP.version_1_2
Net::HTTP.start {|http3| …(http3 has 1.2 features)… }
This function is NOT thread-safe.
This was for backwards-compatibility - something which was taken
seriously between ruby 1.6 and 1.8…
I am using ruby version 1.9.2 and the method is returning a pair of
objects:
Net::HTTPResponse
string
Here’s a successful post, and only a single object is returned. What
does this show on your system?
ruby-1.9.2-p0 > res = nil; Net::HTTP.start(“chart.apis.google.com”,80) {
|http| res = http.post “/chart”,
“cht=lc&chtt=This+is+|+my+chart&chs=300x200&chxt=x&chd=t:40,20,50,20,100”
}
=> #<Net::HTTPOK 200 OK readbody=true>
ruby-1.9.2-p0 > res[“Content-Type”]
=> “image/png”
ruby-1.9.2-p0 > res.body.size
=> 7094
Brian thanks for reply. I tried the code above and am getting the same
result as you.
I must apologize as I meant to say I was using require ‘net/https’ and
doing a HTTPS call e.g. :
resp, data = http.post(path, data, headers)
This is where the two objects are returned. Does ‘net/http’ and
‘net/https’ behave differently?
This is where the two objects are returned. Does ‘net/http’ and
‘net/https’ behave differently?
I don’t think so. Can you show some actual code, preferably something
which POSTs to a public HTTPS server and therefore can be run
standalone?
Brian, I cannot post my example as it is currently posting to an
internal endpoint at work.
The following code sample (changed slightly) from http://snippets.dzone.com/posts/show/788 is the same as I am using. If
you look at the HTTP response it has the following two objects - resp,
responsedata.
resp represents the HTTP Response class and responsedata represents the
body