icontrader
CDCloudflare Developers
•Created by icontrader on 9/11/2024 in #workers-help
525 Error when making API Call over worker
hey there, I get a 525 error when I try to use the DeepL API from my worker. It works just fine locally and the issue only appears once deployed to production. From researching its got something to do with the SSL handshake between CF and the API but I haven't found a solution, anyone know how to solve it?
export async function translateHtml(htmlContent, sourceLang, targetLang, env) {
try {
console.log('Translating HTML content...');
console.log(htmlContent);
const authKey = getEnvironmentVariable('DEEPL_API_KEY', env);
const response = await fetch('https://api-free.deepl.com/v2/translate', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
auth_key: authKey,
text: htmlContent,
source_lang: sourceLang.toUpperCase(),
target_lang: targetLang.toUpperCase(),
tag_handling: 'xml', // Ensure HTML/XML tags are preserved
}),
});
// Check if the response is successful
if (!response.ok) {
const errorText = await response.text();
console.error(
DeepL API error: ${response.status} - ${errorText});
throw new Error(
Translation failed with status ${response.status});
}
const result = await response.json();
return result.translations[0].text;
} catch (error) {
console.error('Error during translation:', error.message || error);
throw error;
}
}
It's a fairly straightforward fetch call with js over the worker. Any help would be massively appreciated1 replies