diff --git a/src/formHandler.js b/src/formHandler.js index 4ab4af9..5af6a8e 100644 --- a/src/formHandler.js +++ b/src/formHandler.js @@ -4,8 +4,16 @@ import fetch from 'node-fetch'; export async function submitToListmonk(req, res) { const { name, email, message } = req.body; + const listmonkUrl = process.env.LISTMONK_URL; - const apiKey = process.env.API_KEY; + const listmonkUsername = process.env.LISTMONK_USERNAME; + const listmonkPassword = process.env.LISTMONK_PASSWORD; + + // Encode username and password as base64 + const auth = Buffer.from(`${listmonkUsername}:${listmonkPassword}`).toString('base64'); + + + console.log(`Sending request to Listmonk with URL: ${listmonkUrl}`); const payload = { email, @@ -14,18 +22,21 @@ export async function submitToListmonk(req, res) { fields: { message } }; + console.log('Sending request to Listmonk with payload:', payload); + try { const response = await fetch(listmonkUrl, { method: 'POST', headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${apiKey}` + 'Content-Type': 'application/json', + 'Authorization': `Basic ${auth}` }, body: JSON.stringify(payload) - }); + }) if (!response.ok) { - throw new Error(`Listmonk API error: ${response.statusText}`); + const errorBody = await response.text(); // Get the response body as text + throw new Error(`Listmonk API error: ${response.statusText}. Response body: ${errorBody}`); } const result = await response.json();