I now have a successful POST request (Query Events, under Wix Events). It takes a “search term” and “limit” from the front end and calls the backend function shown here. It returns a list of all Events with the Event Title containing the search term.
import {fetch} from 'wix-fetch';
import {getApiKey} from 'backend/secrets.jsw';
const eventsApiBaseUrl = 'https://www.wixapis.com/events';
const queryEventsUrl = '/v1/events/query';
let ownerAccountId = '<hidden>';
let siteId = '<hidden>';
export async function queryEvents(searchTerm, limit) {
try {
const body = {
"filter": {
"title": {
"$contains": searchTerm
}
},
"limit": limit
}
const requestBody = JSON.stringify(body);
const accessToken = await getApiKey();
const fetchString = eventsApiBaseUrl + queryEventsUrl; // Query Events
const requestOptions = buildRequest('POST', accessToken, requestBody);
console.log(fetchString);
console.log(requestOptions);
const httpResponse = await fetch(fetchString, requestOptions);
console.log(httpResponse);
if (httpResponse.ok) {
return httpResponse.json();
} else {
return "Fetch did not succeed";
}
} catch (error) {
console.error(error);
}
} // end of function queryEvents
function buildRequest(method, accessToken, requestBody) {
return {
method: method,
headers: {
'Content-Type': 'application/json',
Authorization: accessToken,
'wix-account-id': ownerAccountId,
'wix-site-id': siteId
},
body: requestBody,
};
} // end of function buildRequest