简体   繁体   中英

JavaScript API post and return using Google Sheets script?

I need some help with this. I can't figure out how to print the return from an api post sent from Google Sheets (including any error codes). Posting seems to work fine as the data shows up on RequestBin when I run the function in Sheets (although there are no quotes "" around the nonce output, which I don't understand either).

I need to be able to print the return in Google Sheets (this function runs in the Script Editor) and the data needs to overflow into adjacent cells, whatever the post request returns.

This is the functional code that I have so far:

function testPost() {
  var nonce = new Date().getTime();

  var data = {
    'key':'QrHuXeXcbN',
    'signature':'c4cc0d98be8e0860391799c2ca719da5ea6514f1538c4ec69ec1c19',
    'nonce':nonce
  };

  var options = {
   'method' : 'post',
   'contentType': 'application/json',
   'payload' : JSON.stringify(data)
  };

  var response = UrlFetchApp.fetch('https://requestb.in/19bzt9a1', options);
}

I have tried a number of different ways to return the values but I can't figure it out.

Got it. Finally. Just needed this at the end... return response.getContentText()

function testPost() {
  var nonce = new Date().getTime();

  var data = {
    'key':'QrHuXeXcbN',
    'signature':'c4cc0d98be8e0860391799c2ca719da5ea6514f1538c4ec69ec1c19',
    'nonce':nonce
  };

  var options = {
   'method' : 'post',
   'contentType': 'application/json',
   'payload' : JSON.stringify(data)
  };

  var response = UrlFetchApp.fetch('https://requestb.in/19bzt9a1', options);
  return response.getContentText()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM