Search code examples
node.jsazuretranslationazure-cognitive-services

node-fetch azure document translator


I am trying to use the microsoft api to translate documents but I have issues with the documentation.

I have the following code

import fetch, { FormData, Headers, fileFrom } from 'node-fetch'

async function run() {
    try {
        const testFile = await fileFrom('./data/documen.pdf', 'application/pdf')
        const formData = new FormData()
        formData.set('document', testFile, 'test.pdf')

        const headers = new Headers({
            'Ocp-Apim-Subscription-Key': 'xxx'
        });

        const response = await fetch(
            `https://xxx.cognitiveservices.azure.com/translator/document:translate?targetLanguage=fr&api-version=2024-05-01`,
            {
                method: 'POST',
                headers,
                body: formData
            }
        )

        const result = await response.json();

        console.log(result);

    } catch (error) {
        console.error(error)
    }
}

run()

And it returns

{
  error: {
    code: 'InvalidRequest',
    message: 'The format parameter is not valid.',
    target: 'ContentType',
    innerError: {
      code: 'InvalidFormat',
      message: 'The format parameter is not valid.'
    }
  }
}

Seems that the issue is coming from ContentType, reading the documentation I do not really understand how the document and type query parameters works, why should I set a path if all this information is in the formData


Solution

  • I found a solution, sending a request to the batch endpoint https://xxx.cognitiveservices.azure.com/translator/text/batch/v1.1/batches

    First you have to upload the file into an azure blob container then send a POST request.

    Finally your translated file will be available into the azure blob container