Login

Learning&Support

Documentation

Advanced Scripting | Sample App 5: Using twxhttp

Twixl Support Team Updated: - Created :

In the series Advanced Scripting Sample Apps we explain some use cases on how to use the Advanced Scripting. For more general info, it's very important that you read this KB-article first.

Preview with the Twixl App

You can preview this app by scanning the QR code with the Twixl app.

Replace QR-Code

Use Case

In this example, we show how to use the twxhttp functions. These allow you to make requests to external websites to obtain information from there. Very handy to get info from internal company-tools, e.g. if your Twixl app is a sales tool!

Instructions

Preparations

  1. Create a new app
  2. Set up your Advanced Scripting (see below)
  3. Run the app in the Twixl app and then go to the Gear menu > Advanced Filter Log to see the log messages.
  4. For the Browser Client, open the root collection, append ?debug=1 to the url and you'll see the debug icon in the toolbar.

Advanced Scripting Settings

For the Root Collection:

// Executed once for every collection before the filtering of the items
function setupFilter(collection, environment) {

    // Get a HTTP client
    var client = twxhttp.NewClient();

    // Get the text from a url    
    var textURL = "https://demo.twixlmedia.com/advanced-scripting/sample.txt";
    var textResponse = client.Get(textURL);
    twxlog.Info("Text output: " + textResponse.ToString());
    
    // Get JSON from a url
    var jsonURL = "https://demo.twixlmedia.com/advanced-scripting/sample.json";
    var jsonResponse = client.Get(jsonURL);
    twxlog.InfoDump(jsonResponse.ToJSON());

    // Using the status code
    var badURL = "https://demo.twixlmedia.com/advanced-scripting/sample.invalid";
    var badResponse = client.Get(badURL);
    twxlog.Info("Status code: " + badResponse.StatusCode());

    // HTTP Post
    var postURL = "https://demo.twixlmedia.com/advanced-scripting/post.php";
    var postResponse = client.PostForm(postURL, {"name": "Twixl media", "product": "Twixl Publisher"});
    twxlog.Info("Post output: " + postResponse.ToString());

    // HTTP Raw Post
    var rawURL = "https://demo.twixlmedia.com/advanced-scripting/post_json.php";
    var rawResponse = client.PostRaw(rawURL, "application/json", '{"name": "Twixl media", "product": "Twixl Publisher"}');
    twxlog.Info("Raw Post output: " + rawResponse.ToString());
    
}

// Executed for once for every collection
function shouldShowCollection(collection, environment) {
    return true;
}

// Executed for every single content item
function shouldShowItem(contentItem, collection, environment) {
    return true;
}

// Executed once for every collection after the filtering of the items
function teardownFilter(collection, environment) {
}

Was this article helpful?

0 out of 0 found this helpful