Home > Software design >  doPost(e) with no result - Google script as API
doPost(e) with no result - Google script as API

Time:12-08

I can't seem to get the simplest doPost to reply in Scripts, tried a few things from around the internet, and cannot get a Post to reply. I have reduced it to as simple as possible to get a reply, as below.

function doPost(e) {

   var HTMLString = "<style> h1,p {font-family: 'Helvitica', 'Arial'}</style>" 
   "<h1>Hello World!</h1>"
   "<p>Welcome to the Web App";
 
 HTMLOutput = HtmlService.createHtmlOutput(HTMLString);
 Logger.log(HTMLOutput);
// Return plain text Output
 return ContentService.createTextOutput(HTMLOutput);


}

Deployment settings: Google deployment settings

I am using postman to send the POSTscreenshot of Postman result No result.

Any ideas?

CodePudding user response:

  • I understood your setting of Web Apps from I have edited the question to show the deployment settings are "me" and "anyone".

  • About Not sure what you mean by "Reflect your latest script" to obtain a result sorry, can you explain more?, when you use Web Apps, it is required to prepare a script with Google Apps Script. In this case, when you modify your script, it is required to reflect the latest script to the current or a new version of the deployed Web Apps. Here, when the script is reflected in Web Apps as a new version, the deployment ID is changed. By this, the endpoint of Web Apps is also changed. I thought that in this case, it might not be useful for your situation. So, I proposed to reflect the latest script in Web Apps without changing the endpoint of Web Apps. If you cannot understand it, I thought that this post might be useful. Ref (Author: me)

  • About The goal is to create an API, that will do things, but I haven't been able to get a result for a doPost at all. I have managed to doGet with a result, which is lovely, but I want the Post version., unfortunately, in your current script, an object of "HtmlOutput" is returned by return ContentService.createTextOutput(HTMLOutput);. By this, a text of "HtmlOutput" is returned. If you want to put a value to Web Apps as the POSt method and you want to retrieve the returned value, how about the following modification?

    function doPost(e) {
      return ContentService.createTextOutput(JSON.stringify(e));
    }
    
    • When you modified your script of Web Apps, please reflect the latest script to the Web Apps. In this case, when you want to use the endpoint of Web Apps without changing the endpoint, please check this post.

    • For example, when the HTTP request is run by including the request body of '{"key":"value"}' as a text with the POST method, {"parameter":{},"postData":{"contents":"'{\"key\":\"value\"}'","length":17,"name":"postData","type":"text/plain"},"parameters":{},"contextPath":"","contentLength":17,"queryString":""} is returned.

Note:

  • From your showing script, when you want to return your value of HTMLString, the modified script is as follows. But, in this case, the HTML is returned as text. Please be careful about this. If you want to see the rendered HTML, please use doGet and return return HtmlService.createHtmlOutput(HTMLString);.

    function doPost(e) {
      var HTMLString = "<style> h1,p {font-family: 'Helvitica', 'Arial'}</style>"
          "<h1>Hello World!</h1>"
          "<p>Welcome to the Web App";
      HTMLOutput = HtmlService.createHtmlOutput(HTMLString).getContent();
      return ContentService.createTextOutput(HTMLOutput);
    }
    

Reference:

  • Related