Home > OS >  API request Google Apps Scripts
API request Google Apps Scripts

Time:03-05

I need to request information from api.I tried to make a request with UrlFetchApp.fetch and fetchAll.In both cases i got nothing.Here s my code:

  var request1 = {
   url: "https://seo-fast-audit.p.rapidapi.com/?url="   url,
   method : 'GET',
   params: {url: 'https://docteurseo.fr/'}, 
   headers: {
         "x-rapidapi-host": "seo-fast-audit.p.rapidapi.com",
         "x-rapidapi-key": "KEY"
            }
};
let response = UrlFetchApp.fetchAll([request1])

(here i replaced key)

So what is my problem?Is that problem in async functions or am i requesting not correctly?

Here s API i am using https://rapidapi.com/DocteurSEO/api/seo-fast-audit

CodePudding user response:

If you want to convert the following javascript to Google Apps Script, Ref

var axios = require("axios").default;

var options = {
  method: 'GET',
  url: 'https://seo-fast-audit.p.rapidapi.com/',
  params: {url: 'https://docteurseo.fr/'},
  headers: {
    'x-rapidapi-host': 'seo-fast-audit.p.rapidapi.com',
    'x-rapidapi-key': 'SIGN-UP-FOR-KEY'
  }
};

axios.request(options).then(function (response) {
    console.log(response.data);
}).catch(function (error) {
    console.error(error);
});

how about the following modification?

function myFunction() {
  var url = "https://seo-fast-audit.p.rapidapi.com?url="   encodeURIComponent('https://docteurseo.fr/');
  var option = {
    headers: {
      "x-rapidapi-host": "seo-fast-audit.p.rapidapi.com",
      "x-rapidapi-key": "KEY"
    }
  };
  let response = UrlFetchApp.fetch(url, option);
  console.log(response.getContentText())
}
  • In your script, params is not included in the object for fetch and fetchAll. And, I thought that in your situation, url is required to do the URL encode, and com/?url= is com?url=.

Note:

  • I think that the request of the above Google Apps Script is the same as the top of Javascript. But if an error occurs, please check your KEY again.

  • If an error of 403 forbidden occurs, the site might not be accessed from the Google side. I'm worried about this.

Reference:

  • Related