lundi 2 mars 2015

Nodejs Ajax Call with XMLHttpRequest Header

I am trying to write a script for Hubot to make an AJAX call to Strawpoll.me. I have a cURL command that works exactly how I want but I am having trouble translating that into a Node.js function.



curl --header "X-Requested-With: XMLHttpRequest" --request POST --data "options=1&options=2&options=3&options=4&options=5&title=Test&multi=false&permissive=false" http://ift.tt/1Eav0Sm


Here is what I currently have in my script.



QS = require 'querystring'

module.exports = (robot) ->
robot.respond /strawpoll "(.*)"/i, (msg) ->
options = msg.match[1].split('" "')
data = QS.stringify({
title: "Strawpoll " + Math.floor(Math.random() * 10000),
options: options,
multi: false,
permissive: true
})
req = robot.http("http://ift.tt/1Eav0Sm").headers({"X-Requested-With": "XMLHttpRequest"}).post(data) (err, res, body) ->
if err
msg.send "Encountered an error :( #{err}"
return
msg.reply(body)


The script version is returning {"error":"Invalid request","code":40}


I can't tell what I am doing wrong. Thanks for your help.


Aucun commentaire:

Enregistrer un commentaire