# Automatic HTTP Basic Authentication In Ruby on Rails

**URL:** <https://www.ruby-forum.com/t/automatic-http-basic-authentication-in-ruby-on-rails/90095>\
**Category:** Rails\
**Created:** [March 26, 2007, 1:47am UTC](https://www.ruby-forum.com/t/automatic-http-basic-authentication-in-ruby-on-rails/90095 "2007-03-26T01:47:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/13edae/32.png) [@kiran](https://www.ruby-forum.com/u/kiran)\
**Post date:** [March 26, 2007, 1:47am UTC](https://www.ruby-forum.com/t/automatic-http-basic-authentication-in-ruby-on-rails/90095/1 "2007-03-26T01:47:46Z")

</div>

Hi,

i need to access a url  
[www.sometesturl3456.com/index.php](http://www.sometesturl3456.com/index.php)

It prompts HTTP Authentication window asking username and password. I  
have do automatic HTTP Basic authentication without manual entry of  
username and password, as i am reading the content of page in ruby in  
rails using:

response = open(“[www.sometesturl3456.com/index.php](http://www.sometesturl3456.com/index.php)”).read

Please help me to know how this can be done.

Thanks  
Kiran

---

<div class="post-metadata">

**Author:** ![kiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/13edae/32.png) [@kiran](https://www.ruby-forum.com/u/kiran)\
**Post date:** [March 26, 2007, 6:49am UTC](https://www.ruby-forum.com/t/automatic-http-basic-authentication-in-ruby-on-rails/90095/2 "2007-03-26T06:49:54Z")

</div>

On 3/26/07, Kiran [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> response = open(“[www.sometesturl3456.com/index.php](http://www.sometesturl3456.com/index.php)”).read
> 
> Please help me to know how this can be done.

Well, you may want to use HTTP basic auth mechanism of Net::HTTP  
library.  
Basically this is how i would do it:

require ‘net/http’

req = Net::HTTP::Post.new(url.path)  
req.basic\_auth(“username”,“password”)  
req.body = xml\_query # contents the raw query  
req.content\_type = “application/x-www-form-urlencoded”  
req.content\_length = xml\_query.length

```
result = nil
begin
  timeout(3) do
    _result = Net::HTTP.new(url.host,url.port).start { |http|

```

http.request(req)}  
result = \_result.body  
end  
rescue Timeout::Error  
result = nil  
rescue  
result = nil  
end

But probably you want a much simpler solution, another solution would  
be to use SimpleHttp library.

str = SimpleHttp.get  
URI.parse(“[http://username:removed\_email\_address@domain.invalid](http://username:removed_email_address@domain.invalid)”)

URL:

[http://simplehttp.rubyforge.org/](http://simplehttp.rubyforge.org/)

## – gnufied

There was only one Road; that it was like a great river: its springs  
were at every doorstep, and every path was its tributary.
