-1

I am sending post request to facebook graph api using Httparty gem.
My code is

    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 will send data by converting hash into json.
Problem is at the end point data is receiving differently not as i expected (maybe httparty is not parsing properly).

Someone help me with this. Thanks

Sri Harsha
  • 641
  • 2
  • 8
  • 18
  • What data is the endpoint receiving? Please post more info. – SoAwesomeMan Jun 26 '16 at 18:41
  • What data does the endpoint expect, and what is your code that uses Httparty to send the data? – SoAwesomeMan Jun 26 '16 at 18:43
  • @SoAwesomeMan i added my code to question. I don't know what endpoint is receiving because facebook is not providing data to debug. But i am sure i am sending correct data. Because i tested request with curl and it's working. – Sri Harsha Jun 26 '16 at 18:51
  • Could you execute your curl command with verbose flag `-v` and provide the output? That would help me understand what you're trying to achieve with Httparty. You could remove IDs/credentials from the output. – SoAwesomeMan Jun 26 '16 at 18:57
  • @rock, are you sure that "@page_access_token" is appropriately set before making the request? I guess it would be helpful if you also mention the output of HTTParty.post( ... ). – Harsh Trivedi Jun 26 '16 at 18:58
  • @HarshTrivedi {"error"=>{"message"=>"(#100) Incomplete element data: title and at least one other field (image url, subtitle or buttons) is required with non-empty value", "type"=>"OAuthException", "code"=>100, "fbtrace_id"=>"DKypmESGCUq"}} – Sri Harsha Jun 26 '16 at 19:04
  • @SoAwesomeMan curl -X POST -H "Content-Type: application/json" -d "{\"recipient\":{\"id\":\"1291831200828847\"},\"message\":{\"attachment\":{\"type\":\"template\",\"payload\":{\"template_type\":\"generic\",\"elements\":[{\"title\":\"Titilize\",\"subtitle\":\"Subtitle\"}]}}}}" "https://graph.facebook.com/v2.6/me/messages?access_token=access_token" – Sri Harsha Jun 26 '16 at 19:10

1 Answers1

0

Seems like you have to set the Content-Type header. There may be issues with the Hash syntax depending on your version of Ruby, so check out this open ticket: 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)
SoAwesomeMan
  • 3,226
  • 1
  • 22
  • 25