cjvnjde / google-translate-api-browser

A free and unlimited API for Google Translate
https://www.npmjs.com/package/google-translate-api-browser
MIT License
213 stars 31 forks source link

CORS header ‘Access-Control-Allow-Origin’ missing #37

Closed NyuBlara closed 9 months ago

NyuBlara commented 9 months ago

After attempting to use the package with the default CORS proxy URL (http://cors-anywhere.herokuapp.com/), and encountering a 403 error, I then attempted to use two separate proxy servers : CorsProxy.io and CORS.SH.

With both of these servers, the package reports a NetworkError, and the underlying error is as follows :

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at [PROXY URL]/https://translate.google.com/_/TranslateWebserverUi/data/batchexecute. [...] (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

For reference, the calling code is the following :

const response = await translate(translatedText, {
        to: nextLang,
        corsUrl: 'https://proxy.cors.sh/' // or https://corsproxy.io/
})

This seems to suggest that proper headers are not added to the POST request for proxy servers other than the default.

cjvnjde commented 9 months ago

Thanks for the ticket. I'll take a look at it over the weekend

WilliamFalci commented 9 months ago

@cjvnjde same issue here

cjvnjde commented 9 months ago

In version 4.3, it is now possible to include custom headers.

like so

translate(text, { headers: { something: "something" } })