node-fetch azure 文档翻译器

问题描述 投票:0回答:1

我正在尝试使用 microsoft api 翻译文档,但我对文档有疑问。

我有以下代码

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()

它又回来了

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

似乎问题来自

ContentType
,阅读文档我不太明白
document
type
查询参数是如何工作的,如果所有这些信息都在
formData中,为什么我应该设置路径

node.js azure translation azure-cognitive-services
1个回答
0
投票

我找到了解决方案,向批处理端点发送请求

https://xxx.cognitiveservices.azure.com/translator/text/batch/v1.1/batches

首先,您必须将文件上传到 azure blob 容器中,然后发送 POST 请求。

最后,您的翻译文件将可用于 azure blob 容器

© www.soinside.com 2019 - 2024. All rights reserved.