发送通过HTTParty发送的帖子查询

时间:2012-02-16 11:47:12

标签: ruby-on-rails ruby buffer httparty

我正在使用带有HTTParty的Buffer App API尝试通过/updates/create方法添加帖子,但API似乎忽略了我的“text”参数并引发了错误。如果我在命令行上通过cURL这样做,它可以很好地工作。这是我的代码:

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

我正在运行这样的方法:

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

我已经在课程中添加了debug_output $stdout,它似乎正在发布:

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"

但是我收到了一个错误。我错过了什么吗?

1 个答案:

答案 0 :(得分:19)

我查看了API,并且更新期望JSON位于POST正文中,而不是查询字符串。请尝试使用:body代替:query

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