0

I am trying to access an API in an apps scripts function. I have tested my code in VS code and it works just fine, but when I run it in apps scripts, I get an error saying "ReferenceError: URLSearchParams is not defined". Does anyone know of a fix? None of the similar questions offer a viable solution.

Code:

async function ApiTest() {
let status = "watching";
let num_watched_episodes = 10;
let token = "MyTokenIsHere";
let id = 50346;
const urlParams = new URLSearchParams({
  status: status,
  num_watched_episodes: num_watched_episodes,
});

fetch('https://api.myanimelist.net/v2/anime/' + id + '/my_list_status', {
    method: "PATCH",
    headers: {
        'Authorization': 'Bearer ' + token,
    },
    body: urlParams,
})
}
Red Reaper
  • 51
  • 3
  • 1
    Also related: https://stackoverflow.com/questions/69577647/url-api-parser-alternative-for-google-scripts/69578680#69578680 – TheMaster Oct 28 '22 at 06:42
  • @TheMaster I tried that, but it didn't work. I looked at url.gs and at the end most of it was a comment for some reason. Could you possibly link to a txt file of your url.gs ? – Red Reaper Oct 28 '22 at 19:09
  • 1
    You probably don't need that. See linked duplicates above. There are functions that simulate what you want to do. `URLSearchParams` is used only to convert the object to a query string. But that isn't the only thing wrong here: Read about `urlfetch`. See and test other samples about `urlfetch`. Play with it. – TheMaster Oct 28 '22 at 19:43

0 Answers0