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!