1
Solved

Fetch and XMLHTTPRequest doesn't work in baked applications

 created a very simple fetch PoC that works perfectly fine in the tasker itself, but fails when I run it from a task exported as an app.

Code is a very basic GET request with some params to make sure that it works:

// Provide an address that will get queried:
const apiUrl = 'http://192.168.0.1:8012/';

const isOnMobile = typeof flash === 'function';
// Use flash to communicate from task on phone and console.log on the desktop.
const logFunction = isOnMobile ? flash : console.log;

const requestParams = {
    method: 'GET',
    headers: new Headers( {
        'Authorization': `Bearer abc`,
        'Content-Type': 'application/json',
        'Notion-Version': '2022-02-22'
    } )
};

fetch( apiUrl, requestParams )
    .then( response => {
        logFunction( 'success!' );

        if ( isOnMobile ) {
            exit();
        }
    } )
    .catch( error => {
        logFunction( `Fetch error: ${ error }` );
    } );

I embed it to a task as a JavaScriptLet (to make sure it gets baked in into a file).

It makes the query fine from the Tasker application (when I simply run the task).

However, after exporting it as an app, installing, it just errors out with a flash:

Fetch error: TypeError: failed to fetch meaning that it got to .catch() handler with this generic error.

I used GET specifically as it's the simplest and safest request possible. I don't use builtin Tasker HTTP request feature as my target usage is much more complex POST call to external API based on certain conditions.

---

Since the docs state explicitly:

JSON and XMLHTTPRequest are also directly available from the JavaScript code.

I tried good old XMLHTTPRequest - still no luck, shows a "Transfer failed." toast and no request is being sent (but it works in Tasker).

Code as follows:

// Provide an address that will get queried:
const apiUrl = 'http://192.168.0.1:8012/';

const isOnMobile = typeof flash === 'function';

const logFunction = isOnMobile ? flash : console.log;


// ------------------------- XMLHttpRequest implementation

function reqListener() {
    // console.log(this.responseText);
    logFunction( 'fetched!');
    logFunction( this.responseText.substring( 0, 30 ) );
}

function commonHandler() {
    // Code run by all paths.
    if ( isOnMobile ) {
        exit();
    }
}

const req = new XMLHttpRequest();
req.addEventListener("load", reqListener);
req.addEventListener("load", commonHandler);
req.addEventListener("error", () => logFunction( 'Transfer failed.') );
req.addEventListener("error", commonHandler );
req.addEventListener("abort", () => logFunction( 'Transfer aborted.') );
req.addEventListener("abort", commonHandler );
req.open("GET", apiUrl);
req.send();

---

Android version 12

Tasker 6.1.32

2 replies

M

Yes, that turned out to be missing app manifest permissions that had to be added manually.

I added `android.permission.ACCESS_NETWORK_STATE` and `android.permission.INTERNET` (that's what Chat GPT told me to add 😅) could be that INTERNET alone would work.

I guess that's too much of a hassle to check js code for calling `fetch` or `XMLHTTPRequest` - but maybe the doc should include information about that?

Tasker does not analyze Javascript code, sorry 😅 I'm glad it was that though!

Does the generated app maybe not have the INTERNET permission?

Topic is closed for comments