【问题标题】:Sending a post query sent via HTTParty发送通过 HTTParty 发送的 post 查询
【发布时间】:2012-02-16 11:47:12
【问题描述】:

我正在使用带有 HTTParty 的 Buffer App API 尝试通过 /updates/create 方法添加帖子,但 API 似乎忽略了我的“文本”参数并引发错误。如果我在命令行上通过 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"

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

【问题讨论】:

  • 并非所有错误消息都是一样的。它们通常至少包含问题所在的提示,有时会完整地说明问题。换句话说,请提供它。
  • 啊,好的,给你:"{\"success\":false,\"message\":\"Nice try, but you need to write something!\",\"code\":1004}" 这是一个 API 错误,而不是 HTTP 错误

标签: ruby-on-rails ruby buffer httparty


【解决方案1】:

我查看了API,更新预计 JSON 在 POST 正文中,而不是查询字符串中。试试:body 而不是:query

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 2022-10-26
    • 1970-01-01
    相关资源
    最近更新 更多