Question:
After many trials it seems to me that the fetch method does not work.
Product:
Wix Velo
What are you trying to achieve:
I am trying to send a request to an external API and to receive a response.
What have you already tried:
This is the code in velo:
export const testFetch = webMethod(
Permissions.Anyone,
(inp) => {
let payload = {"var1": "val1",
"key2": inp};
const fetch_options = {
method: "GET",
headers: {
'Content-type': 'application/json'
},
//empty body is always sent
body: "{\"wix\":99}" //JSON.stringify(payload)
};
console.log(fetch_options);
return fetch( "https://flannel-lemon-random.glitch.me/" ,
fetch_options
).then((httpResponse) => {
if (httpResponse.ok) {
//body is always empty here
console.log("response: %o", httpResponse);
return httpResponse.text;// json();
} else {
return Promise.reject('Fetch did not succeed');
}
})
.catch((err) => console.log(err));
}
);
This is the server side code receiving the requests
This is the curl command, that works:
curl -X GET https://flannel-lemon-random.glitch.me/ \
-H "Content-type: text/plain" \
-H "Accept: application/json" \
-d "hola"
Additional information:
When wix-fetch sends a request, the body received on the external side is ALWAYS empty.
I tried GET, POST, application/json, url-encoded… does not seem to matter.