0

Can anyone spot any problems that may explain why the api client is giving me the forbidden error? I know the credentials are correct, as GET requests w the same info in the url work find.

Thank you in advance

 app.get('/translate', (req, res) => {
    var textToTranslate = "Hello friend"
    const targetLanguage = "ES"
    var link = `https://api-free.deepl.com/v2/translate`

    var options = 
    {
      method: 'POST',
      headers: {
        "Host": 'api-free.deepl.com',
        "Content-Length": 54,
        "Content-Type": 'application/x-www-form-urlencoded',
        "User-Agent": "YourApp",
        "Accept": "*/*",

      },
      body: JSON.stringify({
        'auth_key': deeplAccessCode,
        'text': textToTranslate,
        'target_lang': targetLanguage
    }),
    }

  
    return fetch(link, options)
      .then((response) => {
        console.log(response)
        return response.json(); //Transform http body to json
      })
        .then((json)=> {
          res.send(json) //return json to browser
        })
        .catch(e => {
          console.log(e)
          return res.sendStatus(400);
          });
  })

1 Answers1

1

It's probably failing because you're setting your Content-Type of your body to be application/x-www-form-urlencoded (which is correct as per the DeepL API specification) but then you provide a JSON body (which would require content type to be application/json).

You need to provide a URL-encoded body instead, like the part you can also append to the URL after the ?. See also this answer on SO.

Jeehut
  • 20,202
  • 8
  • 59
  • 80