D
D
danilx5562021-03-24 10:48:43
Google Apps Script
danilx556, 2021-03-24 10:48:43

How to send a POST request through Google Apps Script?

I want to send a POST request to one senler mailing service with data inside and still need to pass the key I

605aec7d0d56a522345696.png

wrote the following code below. In payload, I pass the necessary data for the service to add a subscriber to the chatbot. In the header I pass the access key.

function senler(){
var data = {
  'vk_group_id': 163864515,
  'v': 2,
  'vk_user_id': 132528477,
  'bot_id': 356810,
};
var options = {
  'method': 'post',
  'contentType': 'application/json',
  'payload': JSON.stringify(data),
  'headers': {'access_token': 'ee95------------------------------------------6'},
};
var response = UrlFetchApp.fetch('https://senler.ru/api/bots/addSubscriber', options);
Logger.log(response.getResponseCode());
}


I execute the code, it seems like 200 comes in response. But in fact nothing changes in the senler service, it turns out that I'm doing something wrong. Moreover, 200 comes even when I send a request without an access key, and this is clearly something wrong.

605aee624a340705880951.png

Tell me, please, what could be wrong and how to fix it? Thanks in advance!

UPD
Replaced the command in logger.log and now it is already clear that there is some kind of error

605afdadd1127713788127.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
danilx556, 2021-03-24
@danilx556

There was a solution, many thanks to Grigory Boev !
It was necessary to place the key in payload and remove "JSON.stringify"
Working code:

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question