huginn / huginn

Create agents that monitor and act on your behalf. Your agents are standing by!
MIT License
43.12k stars 3.75k forks source link

POSTing to WebhookAgent results in unexpected behavior. #1062

Closed virtadpt closed 9 years ago

virtadpt commented 9 years ago

To get some data into Huginn (a list of URLs, specifically), I'm trying to take advantage of the fact that passing a JSON array to the payload_path option would result in the emission of an event for every element in the array. Here's the POST body I'm using:

{"results": ["http%3A//www.thebestfiles.com/query/little-fucking-little-pictures",
"http%3A//sky.2008records.com/index.htm", "http%3A//www.imdb.com/title/tt0112123/",
"http%3A//weylin.org/index.php%3Foption%3Dcom_booklibrary%26task%3Dview%26catid%3D17%26id%3D4",
"http%3A//vaporizer-info.com/en/vaporizer/arizer-v-tower",
"http%3A//looneytunes.wikia.com/wiki/Pinky_and_The_Brain_%28characters%29",
"http%3A//www.pinkymckay.com/wiring-tiny-brains-to-manage-stress/",
"http%3A//www.avclub.com/article/pinky-and-brains-original-voice-actors-return-swea-207611",
"http%3A//tickets.telegraph.co.uk/sitemap/",
"http%3A//tvtropes.org/pmwiki/pmwiki.php/WesternAnimation/PinkyandTheBrain",
"http%3A//www.record-court.com/default.htm",
"https%3A//en.wikipedia.org/wiki/Pinky_and_the_Brain",
"http%3A//www.tv.com/shows/pinky-and-the-brain/",
"http%3A//metatalk.metafilter.com/23658/Tell-Us-A-Fascinating-Thing-About-Yourself",
"http%3A//pinkyandthebrain.wikia.com/wiki/Pinky",
"http%3A//howtoselldentalpractice.co.uk/about-frank-taylor-associates-practice-sales/",
"https%3A//en.wikiquote.org/wiki/Pinky_and_the_Brain",
"http%3A//www.imdb.com/title/tt0674882/",
"http%3A//realtech.burningbird.net/semantic-web/semantic-markup/building-standards-sand"]}

However, a JSON array is not being received by WebhookAgent. I'm not entirely sure what you'd call this:

{
  "{\"results\": ": {
    "\"http://www.thebestfiles.com/query/little-fucking-little-pictures\",
\"http://sky.2008records.com/index.htm\",
\"http://www.imdb.com/title/tt0112123/\",
\"http://weylin.org/index.php?option=com_booklibrary&task=view&catid=17&id=4\",
\"http://vaporizer-info.com/en/vaporizer/arizer-v-tower\",
\"http://looneytunes.wikia.com/wiki/Pinky_and_The_Brain_(characters)\",
\"http://www.pinkymckay.com/wiring-tiny-brains-to-manage-stress/\",
\"http://www.avclub.com/article/pinky-and-brains-original-voice-actors-return-swea-207611\",
\"http://tickets.telegraph.co.uk/sitemap/\",
\"http://tvtropes.org/pmwiki/pmwiki.php/WesternAnimation/PinkyandTheBrain\",
\"http://www.record-court.com/default.htm\",
\"https://en.wikipedia.org/wiki/Pinky_and_the_Brain\",
\"http://www.tv.com/shows/pinky-and-the-brain/\",
\"http://metatalk.metafilter.com/23658/Tell-Us-A-Fascinating-Thing-About-Yourself\",
\"http://pinkyandthebrain.wikia.com/wiki/Pinky\",
\"http://howtoselldentalpractice.co.uk/about-frank-taylor-associates-practice-sales/\",
\"https://en.wikiquote.org/wiki/Pinky_and_the_Brain\",
\"http://www.imdb.com/title/tt0674882/\",
\"http://realtech.burningbird.net/semantic-web/semantic-markup/building-standards-sand\"": {
      "}": null
    }
  }
}

I'm not sure if I'm misusing the payload_path option, or if JSON parsing isn't working correctly, or what. I can post a copy of my WebhookAgent if necessary.

cantino commented 9 years ago

That's bizarre, but it seems like Rails may be seeing a huge single string being POSTed, not valid JSON data. Are you posting with content type JSON and a payload of the data you showed?

virtadpt commented 9 years ago

Content-type: "application/json"? I don't know. I'll check tonight after work.

Payload of the data I showed? Yes, that was a cut-and-paste of the POST body right out of my application.

cantino commented 9 years ago

I'm fairly confident that you're not sending a valid JSON POST request.

curl -H "Content-Type: application/json" -X POST -d '{"key":"value","key2":"value2"}' http://localhost:3000/foo/bar
virtadpt commented 9 years ago

I've added this header to my POST request. I'll give it a try later today.

virtadpt commented 9 years ago

It works! Please don't close this ticket out until I've had a chance to write the wiki page describing how to use the Webhook Agent.

virtadpt commented 9 years ago

Created a wiki page which explains how to use it: https://github.com/cantino/huginn/wiki/Using-a-Webhook-Agent-with-external-software

cantino commented 9 years ago

Thanks for writing this!

Information POSTed into a Webhook Agent must be correctly formatted JSON. All HTTP requests made to a Webhook Agent must have the Content-Type: application/json HTTP header, otherwise you'll get bizarre results.

This isn't technically true. Rails also supports POSTs in form encoding with a syntax for describing nested structures, but JSON is probably easier.