Sending a post query sent via HTTParty

Pezholio picture Pezholio · Feb 16, 2012 · Viewed 13.7k times · Source

I'm working with the Buffer App API with HTTParty to try and add posts via the /updates/create method, but the API seems to ignore my "text" parameter and throws up an error. If I do it via cURL on the command line it works perfectly. Here's my code:

class BufferApp
    include HTTParty
    base_uri 'https://api.bufferapp.com/1'

    def initialize(token, id)
        @token = token
        @id = id
    end

    def create(text)
        BufferApp.post('/updates/create.json', :query =>  {"text" => text, "profile_ids[]" => @id, "access_token" => @token})
    end 
end

And I'm running the method like this:

BufferApp.new('{access_token}', '{profile_id}').create('{Text}')

I've added debug_output $stdout to the class and it seems to be posting OK:

POST /1/updates/create.json?text=Hello%20there%20why%20is%20this%20not%20working%3F&profile_ids[]={profile_id}&access_token={access_token} HTTP/1.1\r\nConnection: close\r\nHost: api.bufferapp.com\r\n\r\n"

But I get an error. Am I missing anything?

Answer

Mark Thomas picture Mark Thomas · Feb 16, 2012

I reviewed the API, and the updates expect the JSON to be in the POST body, not the query string. Try :body instead of :query:

    def create(text)
        BufferApp.post('/updates/create.json', :body => {"text" => text, "profile_ids[]" => @id, "access_token" => @token})
    end