SimpleHTTP initial release

I just got through putting together a little wrapper around Net:Http
because I keep forgetting how to handle stuff like proxies, ssl, basic
auth and redirects with it. Thought it might be useful to others as
well. Enjoy,
-Tim B. ([email protected])

SimpleHttp - a simplified wrapper around Net::Http

SimpleHttp aims to reduce the complexity of Net::Http while providing
the most commonly used (by me) http functionality.

FEATURES / USAGE

* No fuss one line GET and POST requests:

        str = SimpleHttp.get "http://www.example.com"
        str = SimpleHttp.get "www.example.com"

* Can use URI or String url interchangibly

        str = SimpleHttp.get URI.parse "http://www.example.com/"

* Transparent Proxy Handling. Uses the 'http_proxy' environment

variable if set, also provides a set_proxy method.

        http = SimpleHttp.new "http://www.example.com"
        http.set_proxy "http://proxy.example.com:8000"
        http.post "query" => "example_query"

* POST sends ruby Hashes as 'application/x-www-form/urlencoded'

per default, but can send any data.

        http = SimpleHttp.new "http://www.example.com/image_upload"
        http.post imageData, "img/png"

* Automatically handles SSL

        str = SimpleHttp.get "https://secure.example.com"

* Easy HTTP Basic Authentication

        str = SimpleHttp.get 

URI.parse(“http://usr:[email protected]”)
#or
http = SimpleHttp.new “http://www.example.com
http.basic_authentication “user”, “password”
http.post “query” => “example_query”

* Access headers of the request or response

        http = SimpleHttp.new "www.example.com"
        http.request_header["X-Custom-Header"]="useful header"

* Automatically follows Http Redirects.

The get and post methods return a String containing the body of the
request if the request was successful (HTTP 200). In case of a
redirect, the redirect is followed and the ultimate response is
returned. Per Default, up to three redirects are followed, this
behaviour can be modified by setting follow_num_redirects.

In case of any other type of response, an exception is raised.

The default behaviour can be modified by registering handlers using
the register_response_handler method. E.g. if you’d like to retrieve
the Date header instead of the body for successful transactions:

    http = SimpleHttp.new ...
    http.register_response_handler(Net::HTTPSuccess) {|req,res,http|
            res['date']
    }

Or you’d like to print the Location and then raise an exception in
case of a redirect:

    http = SimpleHttp.new ...
    http.register_response_handler(Net::HTTPRedirect) 

{|req,res,http|
puts res[‘location’]
raise “REDIRECT not allowed!”
}

http://simplehttp.rubyforge.org/
http://rubyforge.org/projects/simplehttp/
gem install simplehttp

cool !

Chers,
zimbatm

On 2/8/07, Jonas P. [email protected] wrote:

cool !

Chers,
zimbatm

Agreed looks very useful.
Can’t wait ti test this soon with our enterprise’s Collaboration Server
(it’s a joke I wont name names, I only call it names;).

Cheers
Robert

very nice… I like it. :slight_smile:

Can’t wait ti test this soon with our enterprise’s Collaboration Server
(it’s a joke I wont name names, I only call it names;).

Hmm, sounds like we might have been colleagues… :slight_smile:

Very nice. I think it may be almost capable enough to serve in place
of HttpAccess2 usage in many cases (which is a rather large lib by
comparison). Have you considered cookie support at all?

T.

Tim B. [mailto:[email protected]] :

SimpleHttp - a simplified wrapper around Net::Http

simple is hard to find these days :slight_smile:
but simplehttp simply rocks!

some small request:

  1. pls include in docs on how to include it :slight_smile:
    it’s require “simple_http”, right?

  2. how about auto completion
    eg, example.com tries -> example.com
    -> www.example.com
    -> w3.example.com

    eg, www.example tries -> www.example
    -> www.example.com
    -> www.example.org
    -> www.example.local

    eg, example tries -> example
    -> example.com
    -> example.org
    -> example.local
    -> www.example
    -> www.example.com
    -> www.example.org
    -> www.example.local

    2a. toggles like eg
    SimpleHttp.auto_complete true

    2b. configures like eg
    SimpleHttp.auto_complete :prefix => “www, w3, www.subnet”
    SimpleHttp.auto_complete :postfix => “com, org, local”
    SimpleHttp.auto_complete :protocol => “http, https, ftp”
    SimpleHttp.auto_complete :port => “80, 8000, 8080”
    SimpleHttp.auto_complete :port_secure => “443, 4443, 4444”

ok, that was not a simple request :slight_smile:

thanks for simplehttp.
kind regards -botp

mea culpa. sorry for the double post. i thought i made a typo on
addressing…

-----Original Message-----

From: Peña, Botp [mailto:[email protected]]

Sent: Friday, February 09, 2007 12:32 PM

To: ruby-talk ML

Subject: Re: [ANN] SimpleHTTP initial release

Tim B. [mailto:[email protected]] :

SimpleHttp - a simplified wrapper around Net::Http

simple is hard to find these days :slight_smile:
but simplehttp simply rocks!

some small request:

  1. pls include in docs on how to include it :slight_smile:
    it’s require “simple_http”, right?

  2. how about auto completion
    eg, example.com tries -> example.com
    -> www.example.com
    -> w3.example.com

    eg, www.example tries -> www.example
    -> www.example.com
    -> www.example.org
    -> www.example.local

    eg, example tries -> example
    -> example.com
    -> example.org
    -> example.local
    -> www.example
    -> www.example.com
    -> www.example.org
    -> www.example.local

    2a. toggles like eg
    SimpleHttp.auto_complete true

    2b. configures like eg
    SimpleHttp.auto_complete :prefix => “www, w3, www.subnet”
    SimpleHttp.auto_complete :postfix => “com, org, local”
    SimpleHttp.auto_complete :protocol => “http, https, ftp”
    SimpleHttp.auto_complete :port => “80, 8000, 8080”
    SimpleHttp.auto_complete :port_secure => “443, 4443, 4444”

ok, that was not a simple request :slight_smile:

thanks for simplehttp.
kind regards -botp

On 2/9/07, Tim B. [email protected] wrote:

Can’t wait ti test this soon with our enterprise’s Collaboration Server
(it’s a joke I wont name names, I only call it names;).

Hmm, sounds like we might have been colleagues… :slight_smile:

that is very much possible given your address (not the gmail one).
I do have problems with the proxy, without a proxy simple_http works
just
fine.

May I suggest that you add setup.rb into the package (root of the
tarball)
http://i.loveruby.net/en/projects/setup/
and maybe a very short INSTALL file to explain to do the classical
ruby setup.rb config
ruby setup.rb setup
sudo ruby setup.rb install

that would get you set for the future.
Sorry I did not test the gem, I did not need gems so far :wink:

Cheers
Robert

fr: Peña, Botp [mailto:[email protected]]

2a. toggles like eg

SimpleHttp.auto_complete true

2b. configures like eg

SimpleHttp.auto_complete :prefix => “www, w3, www.subnet”

SimpleHttp.auto_complete :postfix => “com, org, local”

SimpleHttp.auto_complete :protocol => “http, https, ftp”

SimpleHttp.auto_complete :port => “80, 8000, 8080”

SimpleHttp.auto_complete :port_secure => “443, 4443, 4444”

that wasn’t rubyish enough, so

    SimpleHttp.auto_complete.active true

    SimpleHttp.auto_complete.prefix      # ["www", "w3", 

“www.subnet”]
SimpleHttp.auto_complete.postfix # [“com”, “org”, “local”]
SimpleHttp.auto_complete.protocol # [“http”, “https”, “ftp”]
SimpleHttp.auto_complete.port # [80, 8000, 8080]
SimpleHttp.auto_complete.port_secure # [443, 4443, 4444]

On 2/9/07, Tim B. [email protected] wrote:

On 2/9/07, Robert D. wrote:

I do have problems with the proxy, without a proxy simple_http works
just
fine.

Could you tell me what sort of problems you were having? I just tried
it here with a proxy and it worked fine.

I’ll try to get something through to you offlist, thx a lot.
Robert

Thanks everyone for your replies!

I’ve made a couple of changes and will release a new version soon to
reflect your feedback (most stuff is already in subversion) I’ll
upload a new gem, etc, shortly.

On 2/9/07, Trans [email protected] wrote:

Cookies are supported by way of request and response headers. You have
to handle the cookie data, realms and expiry yourself, though:

ht = SimpleHttp.new ‘www.example.com
ht.get
cookie = ht.response_headers[‘set-cookie’]

It might be possible to do something like this:

SimpleSession {
ht = SimpleHttp.new ‘www.example.com
ht.get
page = SimpleHttp.get ‘www.example.com/index2.html’ **
}

** this call would have cookies automatically set to the values
returned in the first call. Not sure how that would work yet, but it
may be worth pursuing.

On 2/9/07, Peña, Botp [email protected] wrote:

it’s require “simple_http”, right?

correct, I’ve updated the README accordingly.

  1. how about auto completion

sounds like an interesting idea, but I’m not sure how to set up the
heuristics of autocompletion. When would you try alternatives? When
DNS lookup fails for example.com? If you can’t connect to port 80?
On a 404? I’ve got a bit of auto completion already in the you can
leave out the 'http://" bit if you like :slight_smile: Anyhow, it sounds like a
great feature, but I have no idea how I’d implement it, I’m very open
to input, though…

On 2/9/07, Robert D. [email protected] wrote:

I do have problems with the proxy, without a proxy simple_http works just
fine.

Could you tell me what sort of problems you were having? I just tried
it here with a proxy and it worked fine.

May I suggest that you add setup.rb into the package (root of the tarball)
http://i.loveruby.net/en/projects/setup/

I added it to the package. Guess you learn something new every day, I
had no idea setup.rb was it’s own project!

Thanks again!
-tim

Hey, cool! Interface suggestion:

  1. SimpleHttp.get “blahblah”, :proxy => 'feem

The class get method already takes a hash which gets escaped and
added to the end of the url as GET parameters, so that won’t work
out…

  1. SimpleHttp.set_proxy ‘feem’
    5.times { SimpleHttp.get “blahblah” }

adding a class method to set the proxy is a great idea, might also add
something along those lines to add default headers such as cookies…

Thanks for the suggestions.
-tim

Tim B. wrote:

I just got through putting together a little wrapper around Net:Http
because I keep forgetting how to handle stuff like proxies, ssl, basic
auth and redirects with it. Thought it might be useful to others as
well. Enjoy,
-Tim B. ([email protected])
Hey, cool! Interface suggestion:

  1. SimpleHttp.get “blahblah”, :proxy => ‘feem’

  2. SimpleHttp.set_proxy ‘feem’
    5.times { SimpleHttp.get “blahblah” }

Devin

On Feb 8, 7:25 am, “Tim B.” [email protected] wrote:

I just got through putting together a little wrapper around Net:Http
because I keep forgetting how to handle stuff like proxies, ssl, basic
auth and redirects with it. Thought it might be useful to others as
well. Enjoy,

Wonderful library! Fills nicely the gap between open-uri and
Net::HTTP. To follow on with others’ calls for enhancement, do you
think you’ll add file upload support? It’s something that’s relatively
difficult to do with the existing Ruby HTTP libraries, and would be a
very welcome addition.

Also, I noticed that you’re using a separate webrick server to perform
the tests - you might consider using FakeWeb (http://
fakeweb.rubyforge.org/) to build some of the tests. It might be that
SimpleHTTP is sufficiently low-level that mocking the responses isn’t
ideal, but it might help speed up the tests and test development.

b.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs