How do I fix error 400 from my call to deepl webrequest

265 Views Asked by At

I've been working on this, and I believe the problem is somewhere in the following though I can't for the life of me figure out what I did wrong. I'm getting the generic 400 error which makes me thing I'm passing information incorrectly. For context I'm using C# in Unity (no particular reason why, I'm just brushing up on it. I'm aware there are better things to use). I can post the full project code if needed. Any idea what I'm doing wrong?

    private void TranslateText(string textToTranslate, string targetLanguage)
    {
        string deepLApiUrl = "https://api-free.deepl.com/v2/translate";
        string postData = $"source_lang=auto&target_lang={targetLanguage}&text={Uri.EscapeDataString(textToTranslate)}";


        UnityWebRequest webRequest = UnityWebRequest.Post(deepLApiUrl, postData);


        webRequest.SetRequestHeader("Authorization", "DeepL-Auth-Key " + apiKey);


        webRequest.SetRequestHeader("User-Agent", "YourApp/1.2.3");

        StartCoroutine(SendTranslationRequest(webRequest));
    }

I am passing a request to translate a simple sentence from Spanish to English through unity webrequest to deepl but I'm getting an error 400. To me it appears the arguments and syntax are correct but something must be wrong for the error to occur.

1

There are 1 best solutions below

0
Daniel Jones On

You should omit the source_lang parameter to use automatic language detection; the value "auto" will be rejected by the DeepL API. See the docs here: https://www.deepl.com/docs-api/translate-text

If you continue to receive 400 errors, you should inspect the response body, it will include an error message explaining the problem.