D
D
danilx5562021-03-28 16:12:07
Google Apps Script
danilx556, 2021-03-28 16:12:07

How to execute or convert JS script to Google Apps Script?

I want to send POST requests to Google Apps Script.

I tried to write the code myself, but in the end something did not work. Made a recommendation, but it looks like it's written in JavaScript. Since Google has its own code, I do not understand how to use these recommendations.

Here is the code I wrote in the app script which doesn't work:

function senler(){
var data = {
  'access_token': 'ee------------------------------------------------6',
  'vk_group_id': 1-------5,
  'v': 2,
  'vk_user_id': 6------5,
//  'bot_id': 356810,
  'subscription_id': 0
};
var options = {
  'method': 'post',
  'payload': data,
};
var response = UrlFetchApp.fetch('https://senler.ru/api/subscribers/del', options);
Logger.log(response.getResponseCode());
}


I make a request and get an empty request error:
60607f51e10a9496811567.png

I contacted support at the service where I send the post. Here are the recommendations that I was given, told to try. I suspect this code is in JS.
const formData = new FormData();

for(const name in data) {
formData.append(name, data[name]);
}

const response = fetch(url, {
method: 'POST',
body: formData
});


Tell me how to apply this recommendation and adapt it to your code in GAS? Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BasiC2k, 2021-03-28
@BasiC2k

Use a service that logs or shows incoming post requests. Then it will become clear what exactly was sent by your request. For example here .

V
Vladimir, 2021-03-28
@AstraVlad

First, Google Apps Script is JavaScript in its purest form, so you shouldn't worry about it being the "wrong script". And secondly, it is worth reading the official documentation on this issue.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question