title: How can I implement this POST request using HTTParty?
body: <p>I am having difficulty making a POST request to an API endpoint using Ruby's HTTParty library. The API I'm interacting with is the <a href="https://github.com/gittip/www.gittip.com#api">Gittip API</a> and their endpoint requires authentication. I have been able to successfully make an authenticated GET request using HTTParty.</p>
  
  <p>You can see in the example code:</p>
  
  <pre><code>user = "gratitude_test"
  api_key = "5962b93a-5bf7-4cb6-ae6f-aa4114c5e4f2"
  # I have included real credentials since the above is merely a test account.
  
  HTTParty.get("https://www.gittip.com/#{user}/tips.json", 
               { :basic_auth =&gt; { :username =&gt; api_key } })
  </code></pre>
  
  <p>That request works and returns the following as expected:</p>
  
  <pre><code>[
    {
      "amount" =&gt; "1.00",
      "platform" =&gt; "gittip",
      "username" =&gt; "whit537"
    },
    {
      "amount" =&gt; "0.25",
      "platform" =&gt; "gittip",
      "username" =&gt; "JohnKellyFerguson"
    }
  ]
  </code></pre>
  
  <p>However, I have been unable to make a successful POST request using HTTParty. The Gittip API describes making a POST request using curl as follows:</p>
  
  <pre><code>curl https://www.gittip.com/foobar/tips.json \
    -u API_KEY: \
    -X POST \
    -d'[{"username":"bazbuz", "platform":"gittip", "amount": "1.00"}]' \
    -H"Content-Type: application/json"
  </code></pre>
  
  <p>I have tried (unsuccessfully) structuring my code using HTTParty as follows:</p>
  
  <pre><code>user = "gratitude_test"
  api_key = "5962b93a-5bf7-4cb6-ae6f-aa4114c5e4f2"
  
  HTTParty.post("https://www.gittip.com/#{user}/tips.json",
                { 
                  :body =&gt; [ { "amount" =&gt; "0.25", "platform" =&gt; "gittip", "username" =&gt; "whit537" } ],
                  :basic_auth =&gt; { :username =&gt; api_key },
                  :headers =&gt; { 'Content-Type' =&gt; 'application/json' }
                 })
  </code></pre>
  
  <p>The first argument is the url and the second argument is an options hash. When I run the code above, I get the following error:</p>
  
  <pre><code>NoMethodError: undefined method `bytesize' for [{"amount"=&gt;"0.25", "platform"=&gt;"gittip", "username"=&gt;"whit537"}]:Array
    from /Users/John/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http/generic_request.rb:179:in `send_request_with_body'
  </code></pre>
  
  <p>I have tried various other combinations of structuring the API call, but can't figure out how to get it to work. Here's another such example, where I do not user an array as part of the body and convert the contents <code>to_json</code>.</p>
  
  <pre><code>user = "gratitude_test"
  api_key = "5962b93a-5bf7-4cb6-ae6f-aa4114c5e4f2"
  
  HTTParty.post("https://www.gittip.com/#{user}/tips.json",
            {
              :body =&gt; { "amount" =&gt; "0.25", "platform" =&gt; "gittip", "username" =&gt; "whit537" }.to_json,
              :basic_auth =&gt; { :username =&gt; api_key },
              :headers =&gt; { 'Content-Type' =&gt; 'application/json' }
             })
  </code></pre>
  
  <p>Which returns the following (a 500 error):</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;500 Internal Server Error&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;\n        Internal server error, program!\n        &lt;pre&gt;&lt;/pre&gt;  
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I'm not really familiar with curl, so I'm not sure if I am incorrectly translating things to HTTParty.</p>
  
  <p>Any help would be appreciated. Thanks.</p>