【问题标题】:How do i convert Axios to Fetch effectively?如何有效地将 Axios 转换为 Fetch?
【发布时间】:2020-06-22 14:50:51
【问题描述】:

我正在尝试实现微软翻译 API。 (https://rapidapi.com/microsoft-azure-org-microsoft-cognitive-services/api/microsoft-translator-text)
我想将它与对话框流集成,而对话框流不适用于 axios 函数,所以我尝试使用 fetch 使用异步函数。

这段代码运行良好

axios({
   "method": "POST",
   "url": "https://microsoft-translator-text.p.rapidapi.com/translate",
   "headers": {
      "content-type": "application/json",
      "x-rapidapi-host": "microsoft-translator-text.p.rapidapi.com",
      "x-rapidapi-key": "key",
      "accept": "application/json",
      "useQueryString": true
   }, "params": {
      "profanityAction": "NoAction",
      "textType": "plain",
      "to": "hi",
      "api-version": "3.0"
   }, "data": [{
      "Text": "Hello, World"
   }]
})
   .then((response) => {
      console.log(response);
      console.log(response.data[0].detectedLanguage.language);
      console.log(response.data[0].translations[0].text);
   })
   .catch((error) => {
      console.log(error);
   })



但是当我使用 fetch 使用相同的代码时,它会给出错误

async function translate() {
   const response = await fetch(`https://microsoft-translator-text.p.rapidapi.com/translate`, {
      "method": "POST",
      "headers": {
         "content-type": "application/json",
         "x-rapidapi-host": "microsoft-translator-text.p.rapidapi.com",
         "x-rapidapi-key": "key",
         "accept": "application/json",
         "useQueryString": true
      }, "params": {
         "profanityAction": "NoAction",
         "textType": "plain",
         "to": "hi",
         "api-version": "3.0"
      }, "data": [{
         "Text": "Hello, World"
      }]
   });
   const jsonres = await response.json();
   console.log(jsonres);
}
translate();

这是错误

error:
code: 400000
message: "One of the request inputs is not valid."

【问题讨论】:

标签: javascript node.js azure axios fetch


【解决方案1】:

我试过你的代码,没问题。我想,这个问题可能你不用node-fetch

因为我没有订阅这个api,所以我的演示代码得到的响应消息是You are not subscribed to this API.

您需要在文件中创建一个带有{} 的package.json。然后你可以创建test.js文件并粘贴我的代码。

运行:

1.npm i

2.npm i axios

3.npm i node-fetch

4.node test.js

const axios = require("axios");
const fetch = require("node-fetch");
axios({
    "method":"POST",
    "url":"https://microsoft-translator-text.p.rapidapi.com/translate",
    "headers":{
    "content-type":"application/json",
    "x-rapidapi-host":"microsoft-translator-text.p.rapidapi.com",
    "x-rapidapi-key":"82394be932msh7f79ebc35ce1075p136396jsn9b461e528aa9",
    "accept":"application/json",
    "useQueryString":true
    },"params":{
    "profanityAction":"NoAction",
    "textType":"plain",
    "api-version":"3.0"
    },"data":[{
    "Text":"I would really like to drive your car around the block a few times."
    }]
    })
    .then((response)=>{
      console.log(response)
    })
    .catch((error)=>{
      console.log(error.message)
    })

async function translate() {
    const response = await fetch(`https://microsoft-translator-text.p.rapidapi.com/translate`, {
       "method": "POST",
       "headers": {
          "content-type": "application/json",
          "x-rapidapi-host": "microsoft-translator-text.p.rapidapi.com",
          "x-rapidapi-key": "82394be932msh7f79ebc35ce1075p136396jsn9b461e528aa9",
          "accept": "application/json",
          "useQueryString": true
       }, "params": {
          "profanityAction": "NoAction",
          "textType": "plain",
          "to": "hi",
          "api-version": "3.0"
       }, "data": [{
          "Text": "Hello, World"
       }]
    });
    const jsonres = await response.json();
    console.log("async method: "+jsonres.message);
 }
 translate();

【讨论】:

    猜你喜欢
    • 2019-06-24
    • 2021-08-17
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2022-01-15
    • 2016-09-02
    • 2021-05-22
    • 1970-01-01
    相关资源
    最近更新 更多