title: Ruby httpclient: 'create_request': undefined method 'each'
body: <p>I'm green when it comes to Ruby. Right now I'm mucking about with a script which connects to the <a href="https://services.enterprisecloud.terremark.com/cloudapi/explorer#" rel="nofollow">Terremark eCloud API Explorer</a>. I'm trying to use the httpclient gem, but I'm a bit confused as to how I'm supposed to construct my client.</p>
  
  <pre><code>#!/usr/bin/ruby
  
  require "httpclient"
  require 'base64'
  require 'hmac-sha1'
  require 'openssl'
  
  # Method definitions
  def get_date
    # Get the time and date in the necessary format
    result = Time.now.strftime('%a, %d %b %Y %H:%M:%S GMT')
  end
  
  def get_signature(action,date,headers,resource,user,pass)
  
    string_to_sign = "#{action}
  
    #{date}
    #{headers}
    #{resource}\n"
  
    return Base64.encode64(OpenSSL::HMAC.digest('sha1', "#{user}:#{pass}", "#{string_to_sign}"))
  end
  
  # Initial variables
  date = get_date
  domain = "https://services.enterprisecloud.terremark.com"
  password = 'password'
  query = {}
  tmrk_headers = Hash.new
  tmrk_headers['x-tmrk-date: '] = date
  tmrk_headers['x-tmrk-version: '] = '2013-06-01'
  
  uri = '/cloudapi/spec/networks/environments/1'
  url = "#{domain}#{uri}"
  username = 'user@terremark.com'
  verb = 'GET'
  
  signature = get_signature(verb,date,tmrk_headers,uri,username,password)
  
  tmrk_headers['Authorization: '] = "Basic \"#{signature}\""
  
  puts signature
  
  client = HTTPClient.new
  client.get_content(url,query,tmrk_headers)
  </code></pre>
  
  <hr>
  
  <p>EDIT: This is no longer valid as I've moved beyond this error with some help:</p>
  
  <p>Right now I'm not concerned about seeing what is returned from the connection. I'm just looking to create an error-free run. For instance, if I run the script without the <code>client.get_content</code> line it will return to a prompt without issue (giving me the impression that everything ran cleanly, if not uselessly).</p>
  
  <p>How am I supposed to construct this? The <code>httpclient</code> documentation uses the example with external headers:</p>
  
  <pre><code>extheader = [['Accept', 'image/jpeg'], ['Accept', 'image/png']]
  clnt.get_content(uri, query, extheader)
  </code></pre>
  
  <p>I'm making the assumption that the query is the URI that I've defined.</p>
  
  <p>In all reality, it isn't set up right in the first place. I need to be able to include the string in the <code>auth_header</code> variable in the string to be signed but the signature is actually part of the variable. I've obviously created a hole in that regard.</p>
  
  <hr>
  
  <p>Any assistance with this will be more than appreciated.</p>
  
  <p>EDIT2: Removed strace pastebin. Adding Ruby backtrace:</p>
  
  <pre><code>/home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:1023:in `create_request': undefined method `each' for #&lt;String:0x0000000207d1e8&gt; (NoMethodError)
      from /home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:884:in `do_request'
      from /home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:959:in `follow_redirect'
      from /home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:594:in `get_content'
      from ./test.rb:42:in `&lt;main&gt;'
  </code></pre>
  
  <p>EDIT3: Updated script; adding further backtrace after making necessary script modifications:</p>
  
  <p>/</p>
  
  <pre><code>home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:975:in `success_content': unexpected response: #&lt;HTTP::Message::Headers:0x00000001dddc58 @http_version="1.1", @body_size=0, @chunked=false, @request_method="GET", @request_uri=#&lt;URI::HTTPS:0x00000001ddecc0 URL:https://services.enterprisecloud.terremark.com/cloudapi/spec/networks/environments/1&gt;, @request_query={}, @request_absolute_uri=nil, @status_code=400, @reason_phrase="Bad Request", @body_type=nil, @body_charset=nil, @body_date=nil, @body_encoding=#&lt;Encoding:US-ASCII&gt;, @is_request=false, @header_item=[["Content-Type", "text/html; charset=us-ascii"], ["Server", "Microsoft-HTTPAPI/2.0"], ["Date", "Thu, 27 Mar 2014 23:12:53 GMT"], ["Connection", "close"], ["Content-Length", "339"]], @dumped=false&gt; (HTTPClient::BadResponseError)
      from /home/msnyder/.rvm/gems/ruby-2.1.1/gems/httpclient-2.3.4.1/lib/httpclient.rb:594:in `get_content'
      from ./test.rb:52:in `&lt;main&gt;'
  </code></pre>