Send HTTP request


In a JavaScript backend mobile service, server scripts use the node.js request module to make HTTP requests.

The following example validates the specified Twitter username by calling the Twitter REST APIs and only inserts the record when validation succeeds.

function insert(item, user, request) {
    if (!item.twitterScreenName) {
        'You must include a twitter screen name.');
    } else {
        // Get the service configuration module.
        var config = require('mobileservice-config');

        // Get the stored Twitter consumer key and secret. 
        var consumerKey = config.twitterConsumerKey,
            consumerSecret = config.twitterConsumerSecret
        // Get the Twitter access token from app settings.    
        var accessToken= config.appSettings.TWITTER_ACCESS_TOKEN,
            accessTokenSecret = config.appSettings.TWITTER_ACCESS_TOKEN_SECRET;   

        var httpRequest = require('request');
        var url = '' + 
            url: url,
            oauth: {
                consumer_key: consumerKey,
                consumer_secret: consumerSecret,
                token: accessToken,
                token_secret: accessTokenSecret
        }, function(err, response, body) {
            if (err) {
                'Unable to connect to twitter.');
            } else if (response.statusCode !== 200) {
                'No twitter user found with name ' + 
                item.twitterScreenName + '.');
            } else {

For more information on how to obtain credentials to access Twitter, see the section Get your Twitter OAuth credentials section in the Schedule recurring jobs in Mobile Services tutorial. For more information about scheduling script execution, see the Mobile Services script reference