Why is it so hard to create a POST API in Corvid

I’ve spent hours upon hours trying to create a simple fetch that posts a request and displays the response in text, but I just cannot get it to work. When I create this outside of the Wix ecosystem it works perfectly! I try creating it in Zapier, it works perfectly.

I’ve copied this example so many times and it just returns UNDEFINED, every time:
https://www.wix.com/corvid/reference/wix-fetch.WixFetchResponse.html

I’ve tried using this and again I just get UNDEFINED
https://support.wix.com/en/article/corvid-accessing-third-party-services-with-the-fetch-api

You try contacting Wix support and as usual it’s impossible despite the fact we’ve got multiple sites paying £40 a month each with top tier ascend membership on each.

How do I create a simple API that POST’s to a url (no input or headers required), and displays the response key and value as a line of text onto a wix page?

Maybe if you post your code here (backend and front end code) , someone will be able to locate the error.

This is the code on the page - again literally copied from Wix’s examples:

import {getCurrentTemp} from ‘backend/YDFdoPersonalReg’ ;

export function buttonFetchTemp_click(event) {

getCurrentTemp()
.then(temp => $w( “#YDFdoShowResult” ).text = temp + " < response" );
}

This is the backend:

//YDFdoPersonalReg.jsw

import {fetch} from ‘wix-fetch’ ;

export function getCurrentTemp() {

let fullUrl = “thisiswhereiputtheurl” ;

return fetch(fullUrl, {method: ‘POST’ })
.then(response => response.json())
.then(json => json.registration_cookie.temp);
}

Outside of Wix this returns:

HTTP / 1.1 200 OK
Content - Type : application / json
{“registration_cookie”: “0bvb8uqbun93ni9bkgs9arbj”, “registration-id”: 149}

At first it returned UNDEFINED, now the script doesn’t work at all.

See this example:
Expose and Access Site APIs
Use MyApi and MyApiClient to expose and access external APIs.

And the Exposing APIs example.

These are fully working examples that you can open in the Editor and start using. Modify them for your own use.

Thanks Yisrael I will give these a try now.

I’m almost there - the post is returning the registration_cookie key and value now, however I’m still getting undefined when trying present registration_cookie as text in the element #text1

Wix code SDK Warning: The text parameter of “text1” that is passed to the text method cannot be set to null or undefined.

Here is the response from the server:

registration_cookie:
“fb5rgdhb4417cb4jp1bpu02b”

registration-id:
195

Here is the front end code:

$w( ‘#getImageButton’ ).onClick((event) => {
getRandomImage().then((res) => {
console.log(res);
res = JSON.parse(res);

        $w( '#text1' ).text = console.log(res.registration_cookie); 
    }) 
}); 

Here is the back end:

export async function getRandomImage(){
let res = await fetch( “theurl/api/registration_initiate/” , {
“method” : “post” ,
“headers” : {
“Content-Type” : “application/x-www-form-urlencoded”
}});
res = await res.text();
return res
}

Sorry, my mistake I’ve spotted it. I got the code working:

By changing:

$w( ’ #text1 ’ ).text = console.log(res.registration_cookie);

To this:

$w( ‘#text1’ ).text = res.registration_cookie;

Thanks for your help Yisrael!