Successful Fetch Response Returns an Empty JSON Object

Hi,

I’m trying to use the wix-fetch API to call a custom deluge function in Zoho CRM. The fetch call returns an empty object when calling Response.json() and Response.text(). I’ve also run this successfully in Postman. The goal is to take a value from an input field when a button is clicked and pass it to the zoho function as one of the arguments then read and parse the response but I don’t appear to be receiving the response body.

Does anyone know any reasons that a successful fetch would return an empty json object in the response body?

Notes about running a custom zoho function via API

  • API key is passed via a query parameter
  • Required content type is form-data
  • Body must be passed with key ‘arguments’ and the value being the function parameters as a (stringified) json object

Front End Code

import {getMerchantByEmail} from 'backend/aModule';

export function contactFormSubmit_click(event) {
	$w('#contactFormSubmit').disable();
	getMerchantByEmail($w('#contactFormEmail').value)
		.then(json => console.log(json));
	
    $w('#contactForm').changeState("Submitted");
}

Back End Code (aModule.jsw)

export function getMerchantByEmail(email) {
  var {fetch} = require('wix-fetch');
  var FormData = require('form-data');

  var requestBody = new FormData();
  var params = // custom function parameters
  {
    "searchField" : "Email",
    "searchValue" : email,
    "authorization" : 
    {
      "type" : "full",
      "key" : "" 
    }
  };

  requestBody.append("arguments", JSON.stringify(params));

  var requestOptions =
  {
    method: "POST",
    headers: {"Cookie": "0afdefadc8=00ad0429837d682dadcd1fa2d808aa64; _zcsr_tmp=896b032c-074e-4fcd-8771-1196fb6f0fbb; crmcsr=896b032c-074e-4fcd-8771-1196fb6f0fbb","Accept": "application/json"},  //Has been attempted both with and without headers
    body: requestBody,
    redirect: "follow" //Has been attempted both with and without redirect
  };

  fetch("https://sandbox.zohoapis.com/crm/v2/functions/{functionName}/actions/execute?auth_type=apikey&zapikey={APIKey}", requestOptions)
    .then(httpResponse => {
      if(httpResponse.ok){
        return httpResponse.json();
      }else{
        return Promise.reject("Fetch did not succeed\n" + httpResponse.status + ": " + httpResponse.statusText);
      }
    })
    .catch(error => console.log('error', error));;
}

I’ve tried to run this both with and without redirect and with every possible combination of the following headers, (including no headers), with the same result of an empty response body.

  • Cookie
  • content-type (multipart/form-data)
  • Accept (both application/json and */*)

This has also been tried both in Preview and in the Published site with the same result.

Response from Postman (No Headers, form-data body type)

{
    "code": "success",
    "details": {
        "output": "{\"status\":\"failure\",\"message\":\"No records found. Validate email for correctness or create a new Lead.\",\"data\":{\"type\":\"None\",\"count\":0,\"records\":[]},\"log\":\"#####Starting function 'GetMerchantRecord'.#####\\nChecking if searchField, Email, is valid\\nSearching for Merchant data in Zoho CRM using email email@example.com...\\nSearching for Merchant in Contacts\\nNo records found in Contacts... Searching for Merchant in Leads\\nNo records found in Leads... No records found!\\n\"}",
        "userMessage": [
            "Checking if searchField is valid",
            "Valid searchValue detected:  email@example.com",
            "Searching for Merchant data in Zoho CRM using email email@example.com...",
            "Searching for Merchant in Contacts",
            "No records found in Contacts... Searching for Merchant in Leads",
            "No records found in Leads... No records found!"
        ],
        "output_type": "string",
        "id": "1019544000000006015"
    },
    "message": "function executed successfully"
}

Because of this, I know that the zoho function call definitely works. I just don’t know why I’m not getting this output from fetch.

Any help at all would be greatly appreciated!

I’m assuming the URL being used here just had a few parts of it replaced for the purposes of posting it publicly?

Are you getting any response at all? Maybe a status code or error message from Zoho that could help?

Anything showing up in the live site event logs or Google Operations?

I’m assuming the URL being used here just had a few parts of it replaced for the purposes of posting it publicly?

Correct, the url had the function name and api key replaced.

Are you getting any response at all?

Nothing at all in the body via wix-fetch. I checked on Postman and on Make with the same parameters and successfully received the response from Zoho.

I created a scenario on Make to accept the payload from Wix via webhook and then return it as application/json. I’ve confirmed that the scenario works correctly. However, I’m also unable to get a response body on wix when using that webhook.

Maybe a status code or error message from Zoho that could help?

The function is completing successfully and returning the result json in every case that it’s being called, including from wix where I do not receive the body.

See received response here

{
    "code": "success",
    "details": {
        "output": "{\"status\":\"failure\",\"message\":\"No records found. Validate email for correctness or create a new Lead.\",\"data\":{\"type\":\"None\",\"count\":0,\"records\":[]},\"log\":\"#####Starting function 'GetMerchantRecord'.#####\\nChecking if searchField, Email, is valid\\nSearching for Merchant data in Zoho CRM using email email@example.com...\\nSearching for Merchant in Contacts\\nNo records found in Contacts... Searching for Merchant in Leads\\nNo records found in Leads... No records found!\\n\"}",
        "userMessage": [
            "Checking if searchField is valid",
            "Valid searchValue detected:  email@example.com",
            "Searching for Merchant data in Zoho CRM using email email@example.com...",
            "Searching for Merchant in Contacts",
            "No records found in Contacts... Searching for Merchant in Leads",
            "No records found in Leads... No records found!"
        ],
        "output_type": "string",
        "id": "1019544000000006015"
    },
    "message": "function executed successfully"
}

In this json, the "code" is the response code from zoho and the "output" is the return from the custom function. This indicates that the function is being run properly (The failure status in the output can be ignored. This is an internal flag that would be used when parsing the response).

Anything showing up in the live site event logs or Google Operations?

Nothing at all on the site log. Just the empty object. {()}
Unfortunately, my org doesn’t have access to Google Cloud Platform so I can’t check Google Operations.

It could be this then. wix-fetch.fetch() wants the body of the request to be type string.

If that’s not workable for you then it’s always possible to install another library like axios Multipart Bodies | Axios Docs

This worked! I was able to resolve by using axios.postForm.

Thanks for the help!

You’re very welcome!