Version 19


    Overview


    When building an integration service to serve content to a tile or app, you may want to explicitly send content back to your service. One easy way to do this is by using the osapi.http commands (osapi.http.get, osapi.http.post, osapi.http.put, osapi.http.delete). For example, to make an HTTP POST request, you could call osapi.http.post.

     

    var payload = {
        'href' : 'https://my-service-url.com/my-endpoint',
        'format' : 'json',
        'authz' : 'signed',
        'headers' : { 'Content-Type' : ['application/json'] },
        'body' : 'some body here'
    };
    
    osapi.http.post(payload).execute(function(response) { ... } );
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

     

    In this short tutorial, we'll add an HTTP POST call to the list tile example in the Jive Node SDK.

     

    This tutorial assumes you've already created a list tile, as described in Creating a List Tile with the Jive Node SDK.

     

    1. Implement an HTTP POST handler in your service.

     

    The first thing you'll want to do is to make sure that your service can respond to an HTTP POST request. Using the tile-list Jive Node SDK example, create a folder under tiles/{tile-name}/backend/routes named "test" (or whatever name you want to call your POST endpoint). Within that directory, create a file named post.js to handle the POST request. You can use the below sample content, which extracts a subject field and a message field from the request body.

     

    var jive = require("jive-sdk");  
    
    exports.route = function(req, res){
    
        var subject = req.body.subject;
        var message = req.body.message;
    
        console.log("Inside POST handler");
        console.log("Subject: "+subject);
        console.log("Message: "+message);
    
        res.end('{ "result" : "great job" }');
    };
    
    
    
    
    
    
    
    
    
    
    
    
    

     

    2. Add a UI element to initiate the POST request.

     

    If applicable, you may want to update the UI of your tile to initiate the POST. In tiles/{tile-name}/public/action.html, let's add a new button right next to the existing button:

     

    <!-- New button... -->
    <button class="btn btn-primary" id="post-service">POST to Service</button>
    
    <!-- Existing button... -->
    <button class="btn btn-primary" id="create-discussion">Create Discussion</button>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

     

    This will add a button in the tile action dialog (which gets called when a user clicks the "Share State" link on the tile).

     

    3. Make the HTTP POST request.

     

    Now we're ready to make a POST request. Within tiles/{tile-name}/javascripts/action.js, let's add a handler for the button to extract the subject and message from the dialog and send this info in the body of the POST request. Add this code next to the other button click handler:

     

    // New code...
    $("#post-service").click(function() {
        var subject = $("#subject").val();
        var message = $("#message").val();
        var payload = {
            'href' : 'https://my-service-url.com:8090/sampletile-list/test',
            'format' : 'json',
            'authz' : 'signed',
            'headers' : { 'Content-Type' : ['application/json'] },
            'body' : '{ "subject" : "' + subject + '", "message" : "' + message + '" }'
        };
        osapi.http.post(payload).execute(function(response) {
            if (response.error) {
                alert("Error " + response.error.code + " " + response.error.message);
            }
            else {
                alert("Successfully sent data to service.")
            }
        });
    });
    
    // Existing code..
    $("#create-discussion").click(function() {
      ...
      ...
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

     

    Be sure to replace "my-service-url.com" with the actual name of your service.

     

    And that's it! We now are sending data back to our service.

     

    4. Start service, install tile.

     

    To run this example:

     

    1. Follow the deployment instructions for the add-on. This should help you deploy the add-on and start your service.
    2. Put the tile on a page that accepts tiles (as described in Creating a List Tile with the Jive Node SDK).
    3. You can now click "Share State" from your tile, enter some content for the subject and message, and then click POST to Service.
      newpostbutton.png
    4. With the above sample content, your service prints the following lines to the console:
      Inside POST handler
      Subject: Here is the subject.
      Message: Here is the message.