HTTP POST or using Node.js module

Hi,

I am looking to use either the Mailgun or Twilio API to send an e-mail/text message from my application. I am unfamiliar with how to send an HTTP POST request within Kinoma. Would this be something simple to do? Mailgun and Twilio both have Node.js libraries that can be installed to make the HTTP requests. Am I able to install these libraries and somehow include them in my Kinoma project?

Thanks.

Comments

  • Greetings,

    HTTP posts are quite straightforward in KinomaJS. There are several examples of this in our sample code, but the easiest to parse is probably in the main.xml of the DIAL sample. The general technique is this:

    var dataToSend = "whatever data you want to post";
    var url = "http://whateverURL";
    var message = new Message(url);
    message.method = "POST";
    message.requestText = dataToSend;
    message.setRequestHeader("Content-Length", message.requestText.length);
    message.setRequestHeader("Content-Type", "text/plain");
    handler.invoke(message, Message.TEXT); //if you're expecting text back.
    

    — Andy

  • Hi Andy,

    Thanks for the response. I'm planning to use the Twilio API (https://www.twilio.com/docs/api/rest/sending-messages) to send a text message. So far, I have set up the code like this

    Handler.bind("/sendText", {
       onInvoke: function(handler, message){
    
    var url = "https://api.twilio.com/2010-04-01/Accounts/{AccountSID}/Messages"; //will replace AccountSID with actual SID
    var message = new Message(url);
    message.method = "POST";
    message.setRequestHeader("From", "+some-number");
    message.setRequestHeader("To", "+another-number");
    message.setRequestHeader("Body", "Testing Twilio API");
    handler.invoke(message, Message.JSON); 
     },
    onComplete: function(handler, message, json){
      mainColumn.mainLabel.string = json.status;
    }
    });
    

    I am getting an error that reads ### BREAK: invalid character 60!

  • I have also tried this:

    Handler.bind("/sendText", {
    onInvoke: function(handler, message){
    
    var dataToSend = JSON.stringify({"From": "+14152302659", "To": "+14155139884", "Body": "test"});
    var url = "https://api.twilio.com/2010-04-01/Accounts/{AccountSID}/SMS/Messages";
    var message = new Message(url);
    message.method = "POST";
    message.requestText = dataToSend;
    message.setRequestHeader("Content-type", "application/json");
    handler.invoke(message, Message.JSON);
    },
    onComplete: function(handler, message, json){
    mainColumn.timeLabel.string = "complete";
    }
    });
    

    I also get the same error as above.

  • Greetings!

    Looking over the Twilio docs, it appears that they want the post encoded as if it were coming from a HTML form. This means that you should be using the application/x-www-form-urlencoded Content-type. It also means that your data should be encoded like a query. That is, something like: From=+123456789&To=123456789&Body=TestTestTest

    Check out this stack overflow post for more info on that.

  • I made those changes, and I am still getting the error ### BREAK: invalid character 60! What does this error mean?

  • It means that you told he platform to expect JSON data back ( Message.invoke(message, Message.JSON) ) but the data that's coming back isn't actually JSON data. So the parser is choking on some character that wouldn't be in that position in a JSON file.

    You can see what your web service is actually returning by using Message.TEXT instead, then just tracing out that third argument that comes back to onComplete.

  • That, and all your previous responses, was exactly what I needed to know :) Thanks Andy!

  • edited February 2016

    Hi AndyKinoma, Can you please tell me what will be the code for POST request. I want to send some JSON data in the request. For example:

    'url : 127.0.01/abc' 'data to be sent : {lmn: 'rock'}' 'method: POST'

    What will be the code for this example?

    Thank you

Sign In or Register to comment.