Question:
When I run fetch() the server always responds “No API KEY”.
Product:
Wix Editor
What are you trying to achieve:
The following curl works perfectly fine:
curl -X POST https://<deleted>.com/portal/api/v1/login \
-H "Accept: application/json" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "api_key=<deleted>" \
-d "email=<deleted>" \
-d "password=<deleted>"
I added the form-data npm package to Velo.
My back end code looks like this:
import wixSecretsBackend from "wix-secrets-backend";
import { fetch } from "wix-fetch";
import FormData from 'form-data';
export const login = webMethod(
Permissions.Anyone,
async (email, password) => {
const key = await getSecret("blah");
const form = new FormData();
form.append('api_key', key);
form.append('email', email);
form.append('password', password);
const fetchOptions = {
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json"
},
body: form
};
const response = await fetch(
"https://<deleted>.com/portal/api/v1/login",
fetchOptions
)
.then((response) => response.json())
.then((json) => {
console.log(JSON.stringify(json));
return json;
});
},
);
The server always responds “No API KEY”.
I triple-checked that the url, the email, the password and the api key are all identical. Any recommendations for how I can fix the code?