title: How do I write a script to call a REST API?
body: <p>I want to write a script which calls a REST API using a URL like:</p>
  
  <pre><code>http://localhost:3000/api/v1/user/xyz
  </code></pre>
  
  <p>If I open this URL in the browser, it asks for a user email and password. Once I've authenticated, it returns a JSON response with a user id. I don't know how I should I authenticate through the script. Can someone give me a suggestion?</p>
  
  <p>This is what I started with:</p>
  
  <pre><code>require 'rubygems'
  require 'net/http'
  require 'json'
  
  api_url = "http://localhost:3000/api/v1//user/xyz"
  
  response = Net::HTTP.get_response(URI.parse(api_url))
  data = JSON.parse(response.body)
  fields = data.keys
  puts fileds
  puts data.keys
  </code></pre>
  
  <p>This is the authentication for the API:</p>
  
  <pre><code> before_filter :authorize, :except =&gt; [:keys]
  private
  
        def authorize
          authenticate_or_request_with_http_basic do |username, password|
            user = User.find_by_email(username)
  
            if user &amp;&amp; user.valid_password?(password)
              sign_in :user, user
            end
          end
        end
  </code></pre>
  
  <p>How I can pass email and password to authenticate this API so I can get "id" as return?</p>