Sending a post query sent via HTTParty

14,090

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
Share:
14,090
Pezholio
Author by

Pezholio

Updated on June 04, 2022

Comments

  • Pezholio
    Pezholio about 2 years

    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?

    • Mark Thomas
      Mark Thomas over 12 years
      Not all error messages are alike. They usually contain at least a hint of what the problem is, and sometimes spell it out completely. In other words, please provide it.
    • Pezholio
      Pezholio over 12 years
      Ah, OK, here you go: "{\"success\":false,\"message\":\"Nice try, but you need to write something!\",\"code\":1004}" This is an API error, rather than a HTTP one obviously