首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过DeepL api向DeepL api发出不起作用的请求

通过DeepL api向DeepL api发出不起作用的请求
EN

Stack Overflow用户
提问于 2021-07-17 18:21:37
回答 1查看 234关注 0票数 0

有人能发现任何可能解释为什么api客户端给我禁止的错误的问题吗?我知道凭证是正确的,因为GET请求--在url工作查找中--有相同的信息。

提前谢谢你

代码语言:javascript
复制
 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);
          });
  })
EN

回答 1

Stack Overflow用户

发布于 2022-02-21 14:08:10

它可能失败了,因为您将身体的Content-Type设置为application/x-www-form-urlencoded (根据DeepL API规范是正确的),但是提供了一个JSON (这需要内容类型为application/json)。

您需要提供一个URL编码的正文,就像您也可以在?之后附加到URL的部分一样。也见这个答案 on SO。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68423213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档