0

我是一个使用 HTTPS 的初学者,但学到了很多东西,并仔细阅读了 DeepL 文档和关于这个主题的所有其他 stackoverflow 问题。

我无法使用 POST 请求从 API 检索信息。我可以只使用 url 参数运行等效的 GET 请求,所以我知道这不是身份验证问题。当我从 fetch 请求中完全删除 options 变量时,我得到了相同的非功能性行为,因此 api 将其视为不可读。有人可以帮忙吗?我不能再使用 GET 方法,因为我的项目需要长于最大 URL 大小(2000~)的查询字符串

 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);
          });
  })

我从我的节点终端收到此错误

FetchError: invalid json response body at https://api-free.deepl.com/v2/ reason: Unexpected end of JSON input
>      at /Users/divineo/Documents/GitHub/Karaoke/functions/node_modules/node-fetch/lib/index.js:272:32
>      at processTicksAndRejections (internal/process/task_queues.js:95:5) {
>    type: 'invalid-json'
>  }

我从控制台日志响应中收到此错误

[Symbol(Response internals)]: {
>      url: 'https://api-free.deepl.com/v2/',
>      status: 403,
>      statusText: 'Forbidden',
>      headers: Headers { [Symbol(map)]: [Object: null prototype] },
>      counter: 1
>    }
4

0 回答 0