How do i convert Axios to Fetch effectively?

Viewed 693

I'm trying to implement microsoft translation API. (https://rapidapi.com/microsoft-azure-org-microsoft-cognitive-services/api/microsoft-translator-text)
I want to integrate it with dialog flow and dialog flow doesn't work with axios function so i tried using async function using fetch.

This code is working fine

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



But when I use the same code using fetch, its giving the error

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

This is the error

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

I have try your code, that's fine. I think, the issue may you don't use node-fetch.

Because I don't subscribed to this api, so my demo code get the response message is You are not subscribed to this API.

You need create a package.json with {} in the file. Then you can create test.js file and paste my code.

Run:

1.npm i

2.npm i axios

3.npm i node-fetch

4.node test.js

enter image description here

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