Mobile client-only 3rd party REST calls

I have created an Android app using Meteor that only has a client side (no Meteor server side). The app uses a 3rd party geospatial database/server and REST API to obtain a token and then posts user location data. The app functions as expected in development (run from meteor on Android device) but not in production (install signed APK on Android device). The POST requests do not work in production.

After reading about Meteor and REST I am unclear if making a REST call from the client is possible (not using Meteor.methods on a Meteor server). I have tried a few different ways to implement the POST request, all of which work in development but not in production:

jQuery AJAX - does not appear to use success or error functions
Meteor HTTP ( Meteor.http.call ) - response is undefined
ESRI-Leaflet ( L.esri.post ) - code: 500; message: “Could not parse response as JSON. This could also be caused by CORS or XMLHttpRequest error.”; response is null

I have also tried editing the mobile-config.js file using various permutations like so:

App.accessRule("*");
App.accessRule(‘https://the_address:the_port’, { type: ‘network’ });
//App.accessRule(‘https://the_address:the_port/arcgis’, { type: ‘network’ });
//App.accessRule(‘https://the_address:the_port/arcgis/tokens’, { type: ‘network’ });
//App.accessRule(‘https://the_address:the_port/arcgis/tokens/generateToken/’, { type: ‘network’ });

After a few straight days of trying to determine if the exact problem is in the code, Meteor, CORS, or something else, I feel I have exhausted all avenues. I am not a CORS guru and I am not a server manager, etc so I am not completely sure how to troubleshoot. From the results listed above, it sounds like the requests are not being sent or returned.

Below is some sanitized code for three versions of a POST function to retrieve a token that I have tried. Each function works in development. I assume since the app works in development it should work in production but likely with some modification in the code or updates to settings/parameters on the server. However, I am unsure of what needs to change.

Any help is much appreciated!

var getToken_ajax = function() {

    var tokenData = {
        username: "theusername",
        password: "thepassword",
        f: 'json',
        expiration: 60,
        client: 'referer',
        referer: window.location.origin
    };

    $.ajax({
        url: "https://<address>:<port>/arcgis/tokens/generateToken",
        dataType: 'json',
        type: 'POST',
        data: tokenData,
        crossDomain: true,
        success: function(data, textStatus, jqXHR) {

            $(".status").empty();
            $("#status").empty().append("status: " + textStatus);
            $("#data").empty().append("data: " + JSON.stringify(data));
            $("#jqXHR").empty().append("jqXHR: " + JSON.stringify(jqXHR));

        },
        error: function(data, textStatus, jqXHR) {

            $(".status").empty();
            $("#status").empty().append("status: " + textStatus);
            $("#data").empty().append("data: " + JSON.stringify(data));
            $("#jqXHR").empty().append("jqXHR: " + JSON.stringify(jqXHR));

        }
    });

};

var getToken_meteorhttp = function() {

    var tokenData = {
        headers: {
            "Content-type": "application/x-www-form-urlencoded"
        },
        params: {
            username: "theusername",
            password: "thepassword",
            f: 'json',
            expiration: 60,
            client: 'referer',
            referer: window.location.origin
        }
    };

    Meteor.http.call("POST", "https://<address>:<port>/arcgis/tokens/generateToken", tokenData,
        function(error, response) {

            $(".status").empty();
            $("#status").empty().append("status: " + JSON.stringify(error));
            $("#response").empty().append("response: " + JSON.stringify(response));

        });
};

var getToken_leaflet = function() {

    function serverAuth(callback) {
        L.esri.post('https://<address>:<port>/arcgis/tokens/generateToken', {
            username: "theusername",
            password: "thepassword",
            f: 'json',
            expiration: 86400,
            client: 'referer',
            referer: window.location.origin
        }, callback);
    };

    serverAuth(function(error, response) {

        $(".status").empty();
        $("#status").empty().append("status: " + JSON.stringify(error));
        $("#response").empty().append("response: " + JSON.stringify(response));

    });

};

Turns out there was not a proper certificate in place. Once in place, everything worked as expected.

1 Like