接收端JSON数据值的变化

时间:2016-06-26 18:31:30

标签: ruby-on-rails ruby json facebook-graph-api httparty

我正在使用 Httparty gem向facebook图表api发送帖子请求 我的代码是

    message_data ={
        "recipient" => {
            "id" => recipient_id
        },
        "message" => {
            "attachment" => {
                "type" => "template",
                "payload" => {
                    "template_type" => "generic",
                    "elements" => [
                        {
                            "title" => "Titilize",
                            "subtitle" => "Subtitle"
                        }
                    ]
                }
            }
        }
    } 
options ={
            "query": {access_token: @page_access_token},
            "body": message_data
        }
HTTParty.post("https://graph.facebook.com/v2.6/me/messages",options)

Httparty 会通过将哈希转换为json来发送数据 问题是在结束点数据的接收方式与我预期的不同(可能 httparty 未正确解析)。

有人帮我这个。 感谢

1 个答案:

答案 0 :(得分:0)

好像你必须设置Content-Type标题。根据您的Ruby版本,Hash语法可能存在问题,因此请查看此打开的票证:https://github.com/jnunemaker/httparty/issues/472

str = "{\"recipient\":{\"id\":\"1291831200828847\"},\"message\":{\"attachment\":{\"typ‌​e\":\"template\",\"payload\":{\"template_type\":\"generic\",\"elements\":[{\"titl‌​e\":\"Titilize\",\"subtitle\":\"Subtitle\"}]}}}}"

options {
  :headers => {"Content-Type" => "application/json"},
  :query => {access_token: @page_access_token},
  :body => str
}

HTTParty.post("https://graph.facebook.com/v2.6/me/messages", options)