2

After 1 month, I would like to open the question regarding this topic once again: Here is the whole post with all links: https://docs.google.com/document/d/1Tb0-twzHl-wXbvaNF2IpCT0CiONT9PoqPlEweLz3oYI/edit

There is a function in a project in which a urlFetchApp sends payload, using doPost in the second script function. The second function stores the payload in the 1. spreadsheet: When running the first function, the second function does not store the payload in the spreadsheet. Detailed description below:

1. project with script

function merry2script() {
  var url = 'https://script.google.com/macros/s/AKfycbzM97wKyc0en6UrqXnVZuR9KLCf-UZAEpzfzZogbYApD9KChnnM/exec';
  var payload = {payloadToSend : 'string to send'};
  var method = 'post'
  var response = UrlFetchApp.fetch(url, {method : method, payload: payload}).getContentText();
  Logger.log(response);
  return;
}

img merry2.jpg

2. project with script

function doPost(e) {
  var ss = SpreadsheetApp.openById("0Apjz67q9b5PldFJUYkkzVGRHdGFYc1pFYWk5T0Eyc0E");
  var sheet = ss.getSheetByName("testsheet");
  var record;
  for (var i in e.parameters) {
    record = 'parameter: ' + i + ' = ' + e.parameters[i];
    sheet.getRange(sheet.getLastRow() + 1, 1, 1, 1).setValue(record);
  }
  var output = ContentService.createTextOutput();
  output.setContent("content to return");
  return output;
}

img merry_christmas.jpg

published 2. script ,

img evidence of publishing 2. script : mch1.jpg

1. spreadsheet https://docs.google.com/spreadsheet/ccc?key=0Apjz67q9b5PldFJUYkkzVGRHdGFYc1pFYWk5T0Eyc0E

Results: When the payload is sent through hurl.it(a webpage), it is not shown in the spreadsheet. trying without result

trying with this option install this chrome extension "Advanced REST Client" client/hgmloofddffdnphfgcellkdfbfbjeloo img result

result in spreadsheet ss_test.jpg

Nakilon
  • 34,866
  • 14
  • 107
  • 142
user1507256
  • 21
  • 1
  • 3
  • My first suggestion would be to `Logger.log` everything you can, and email it to try and figure out what's going on at each step. What are you getting as a response from the `UrlFetchApp.fetch`? – fooby Dec 27 '12 at 17:28
  • The UrlFetchApp.fetch responds this: Everything is here at the end of the document: https://docs.google.com/document/d/1Tb0-twzHl-wXbvaNF2IpCT0CiONT9PoqPlEweLz3oYI/edit Welcome to Google Docs – user1507256 Jan 02 '13 at 13:50
  • It looks like the output what truncated. The log can only take so many characters. To get the full document, try emailing the results of the `UrlFetchApp.fetch`. What you got didn't even get passed Google's CSS. Which might indicated that you are really getting an error page. Have you checked the response code? – fooby Jan 03 '13 at 00:48

2 Answers2

1

In order to get response from the published URL it must be published with following settings:

  1. Execute the app as: me
  2. Who has access to the app: Anybody even anonymous

It is required because, the way you are fetching the URL, it can access only public available web address.

Hari Das
  • 10,145
  • 7
  • 62
  • 59
1

You just need to include the headers key into your dictionary that's all And of course on top of what Hari Das mentioned

headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()}

function merry2script() {
      var url = 'https://script.google.com/macros/s/AKfycbzM97wKyc0en6UrqXnVZuR9KLCf-UZAEpzfzZogbYApD9KChnnM/exec';
      var payload = {payloadToSend : 'string to send'};
      var method = 'post'
      var headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()}
      var response = UrlFetchApp.fetch(url, {method : method, payload: payload, headers: headers}).getContentText();
      Logger.log(response);
      return;
    }
francis
  • 75
  • 6