title: How to send binary data through a HTTP request using a Ruby gem?
body: <p>I'm trying to find out a way to reproduce a HTTP request that sends binary data in the payload as well as sets a <code>Content-Type: binary</code> header, like the following command with cURL:</p>
  
  <pre><code>echo -e '\x14\x00\x00\x00\x70\x69\x6e\x67\x00\x00' | curl -X POST \
  -H 'Content-Type: binary' \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip,deflate,sdch' \
  -H 'Accept-Language: en-US,en;q=0.8,pt;q=0.6' \
  -H 'Cookie: JSESSIONID=m1q1hkaptxcqjuvruo5qugpf' \
  --data-binary @- \
  --url 'http://202.12.53.123' \
  --trace-ascii /dev/stdout
  </code></pre>
  
  <p>I've already tried using the REST Client (<a href="https://github.com/rest-client/rest-client" rel="noreferrer">https://github.com/rest-client/rest-client</a>) and HTTPClient (<a href="https://github.com/nahi/httpclient" rel="noreferrer">https://github.com/nahi/httpclient</a>), but unsuccessfully. Using the code below the server responded with HTTP 500. Has anyone done it before or is it not possible for the purpose to which the gems were designed?</p>
  
  <p>Ruby code:</p>
  
  <pre><code>require 'rest-client'
  request = RestClient::Request.new(
    :method =&gt; :post, 
    :url =&gt; 'http://202.12.53.123', 
    :payload =&gt; %w[14 00 00 00 70 69 6e 67 00 00], 
    :headers =&gt; {
      :content_type =&gt; :binary,
      :accept =&gt; '*/*',
      :accept_encoding =&gt; 'gzip,deflate,sdch',
      :accept_language =&gt; 'en-US,en;q=0.8,pt;q=0.6',
      :cookies =&gt; {'JSESSIONID' =&gt; 'm1q1hkaptxcqjuvruo5qugpf'}
    }
  )
  request.execute
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE</strong> (w/ one possible solution)</p>
  
  <p>I ended up running the request with the HTTParty (following the direction given by @DemonKingPiccolo) and it worked. Here's the code:</p>
  
  <pre><code>require 'httparty'
  hex_data = "14 00 00 00 70 69 6e 67 00 00"
  response = HTTParty.post(
    'http://202.12.53.123', 
    :headers =&gt; {
      'Content-Type' =&gt; 'binary',
      'Accept-Encoding' =&gt; 'gzip,deflate,sdch',
      'Accept-Language' =&gt; 'en-US,en;q=0.8,pt;q=0.6'
    },
    :cookies =&gt; {'JSESSIONID' =&gt; 'm1q1hkaptxcqjuvruo5qugpf'},
    :body =&gt; [hex_data.gsub(/\s+/,'')].pack('H*').force_encoding('ascii-8bit')
  )
  puts response.body, response.code, response.message, response.headers.inspect
  </code></pre>
  
  <p>The body can also be written as suggested by @gumbo:</p>
  
  <pre><code>%w[14 00 00 00 70 69 6e 67 00 00].map { |h| h.to_i(16) }.map(&amp;:chr).join
  </code></pre>