Unable to handle the request on live site

Question:
Hello, I’m working on fetching an API call, all is working properly in my preview but if I publish the site, I got the following error:

createConsoleProxy.ts:47 Error: Unable to handle the request. Contact the site administrator or view site monitoring logs for more information.

Product:
Editor X

What are you trying to achieve:
API Call to get information from 3rd party site.

What have you already tried:
Check permissions, read similar forum information, google it. Nothing worked.

Additional information:
[Include any other pertinent details or information that might be helpful for people to know when trying to answer your question.]

Hey Freddy!

Is it a HTTP API or HTTPS? Which API is it and how and where is it being used in your website? Posting your code here may also help.

Hi Pratham, thanks for your response. It is a POST call to an HTTPS end point, then I manage the response. All is working properly in the preview version but if I publish the site, I get the error described above.

Please find below my code:

import { getAuthCode, getRegions } from 'backend/newDataFetcher';

// See the dataFetcher.jsw web module in the backend for the fetch calls //


$w.onReady(function () { 
   // setupInitialUI();

    $w('#button2').onClick(onButton2Click);
    
});

async function onButton2Click() {
    $w('#button2').disable();
    await getDrowpdownFilled();
    $w('#button2').enable();
}



//Get dropdownfilled
async function getDrowpdownFilled() {
        const regions = await getRegions();
        // Extracting 'descr' fields
        const descrFields = regions.data.map(item => {
                    return {
                        label: item.descr,
                        value: item.id // You can use a different field as the value if needed
                    };
                });
            $w('#dropdown1').options = descrFields;
     
}

//Get authcode
async function getAuthCodeFront() {
    //console.log("before putResponse");
    const putResponse = await getAuthCode();
}
// Sample backend code in a multiplication.jsw file:
import { fetch, getJSON } from 'wix-fetch';
// CONST
const urlRegion = "https://www.irc-com.it/irccombo/anagrafica/tabelle/ListTabella";

// Var
let authToken = "non-auth-code-yet";

const bodyAuth = {
        request: 'login',
        lang: 'it',
        username: ‘XXXXXXXXXXXXXX’,
        password: ‘XXXXXXXXXXXXXXx’
    };

const fetchOptionsAuth = {
        method: 'post',
        headers: 'application/json',
        body: JSON.stringify(bodyAuth)
    };



// POST call to get authentication token. 
export async function getAuthCode() {
    console.log("Generating Auth Code");
    const response = await fetch(urlAuth, fetchOptionsAuth); 
        if (response.ok) {
            const data = await response.json(); // Parse the JSON response
            authToken = data.token; // Replace 'node' with the key of the specific node you want   
            // Use the specificNode data
            console.log('Auth Code Generated');
            } else {
            console.error('Failed to fetch data');
            }

    return authToken;
}

// POST call to get regions 
export async function getRegions() {
    console.log("Getting regions");
    //console.log("Generating Auth Code");
    authToken = await getAuthCode();

    // Region consult body
    let bodyRegion = {
        "model_name": "regioni",
        "page": 1,
        "limit": 10000,
        "order": "asc",
        "token": authToken
    };
    
    // Region fetch options
    const fetchOptionsRegion = {
        method: 'post',
        headers: 'application/json',
        body: JSON.stringify(bodyRegion)
    };

    const response = await fetch(urlRegion, fetchOptionsRegion); 
    return response.json();

}

Thanks for sharing the code!

Did you check the console.logs on the preview and the live site simultaneously? Since you’ve added them throughout the code, checking the console may help you find exactly where the error is occurring.

@anthony or @thomasj It’ll be great if you can chip in on this!

Sure, I did check the logs in both environments.

Here a successful payload for the preview site:

"root":{
"insertId":"..........Dv1gixJrlCt8nhvOhdBV5z"
"timestamp":"2024-01-01T10:37:06.496Z"
"labels":{
"siteUrl":"https://ndvinternational.wixsite.com/my-site-4"
"revision":"-1"
"namespace":"Velo"
"tenantId":"1df7ac7b-3509-4ab8-9d46-d90c98e55e5b"
"viewMode":"Preview"
}
"sourceLocation":{
"file":"backend/newDataFetcher.jsw"
"line":39
"column":12
}
"operation":{
"id":"1704105425.3944374477466515731"
"producer":"backend"
}
"jsonPayload":{
"message":"["Auth Code Generated"]"
}
"receiveTimestamp":"2024-01-01T10:37:06.561Z"
}

Here the rejected payload in the live site:

"root":{
"insertId":"..........Q8xK55VTlB4OltN86.LKEG"
"timestamp":"2024-01-01T10:46:05.192Z"
"labels":{
"siteUrl":"https://ndvinternational.wixsite.com/my-site-4"
"revision":"268"
"namespace":"Velo"
"tenantId":"1df7ac7b-3509-4ab8-9d46-d90c98e55e5b"
"viewMode":"Site"
}
"operation":{
"id":"1704105963.0191389540685112198"
"producer":"backend"
}
"jsonPayload":{
"message":"["invalid json response body at https://www.irc-com.it/irccombo/anagrafica/tabelle/ListTabella reason: Unexpected token < in JSON at position 0"]"
}
"severity":"ERROR"
"receiveTimestamp":"2024-01-01T10:46:05.327Z"
}

I have more information, the API is using cloudflare and for some reason in the live site I getting a “wait a second” html display and not the direct response from the API.

Does anyone knows why this behavior is not the same for the preview site? is there a way to configure the live site to behave as the preview site for the API fetch?

This might be the cause of the issue I guess.

But in your code, you’ve used await so that should’ve taken care of the issue…
Which is why I’ve tagged the devs from the Wix team to shed some light on this, since they can access your site directly.

Or you can try reaching out to the Velo support team here.

Thanks Pratham, I’ll contact support.

It seems the issue is on Cloudflare’s side then. You’ll need to see what options they offer for not replying with a captcha.

Hi @anthony thanks for your response.

I’m confused, the code and server response works properly in the preview mode but not in the live site. Do you have an idea on why this is happening?

I’m not sure why. Just a guess would be that the preview and live backend functions are being executed from different buckets with different IPs and this might feed into why cloudflare is choosing to captcha one and not the other.

Ultimately though the endpoint you’re querying is running on cloudflare so there’s not much we can help debug here since cloudflare would be making the decision on whether to serve the response or a captcha. The captcha issue would need to be solved on cloudflare’s end.