Search code examples
google-apps-scriptweb-applicationspostmanquery-string

Google Apps Script doGet() function not recognizing query string


I am communicating with google apps script with the following:

$.get(url, "name=alice&n=1&n=2", function(data) {
        ...

}).fail(function(xhr, status, error) {
        ...
});

The outgoing request url is:

.../exec?name=alice&n=1&n=2

The problem I am encountering is that on the apps-script side, the doGet function doesn't seem to be recognizing the existence of the query string. I have the following doGet function:

function doGet(e) {
  var groupNumber = e.queryString;
  if (groupNumber != null) {
      return A
  } else {
      return B
  }
}

The problem I am encountering is that the variable groupNumber is always getting set to null and thus the B is always getting returned, and not A. I have saved and deployed the app script and am using the up-to-date url.


Solution

  • I've created an example in Google Apps Script, maybe this could help you find if something is missing.

    • If you want to use queryString it will return a string and you should split the string to extract the values.

    • If you use parameter you can destructure the key you pass through the url.

    Code Example

    function doGet(e) {
      const { name } = e.parameter; // < -- Extracting the key of the query parameter.
    
      const queryString = e.queryString; // < -- Reading the query as a String.
    
      const res = `queryString: ${queryString} <br> name: ${name}`;
      
      return HtmlService.createHtmlOutput(res);
    }
    

    URL Example using queryString & parameter

    https://script.google.com/macros/s/AKfycbyeHSAQCAwhozU0hoLYf0e0jgXV7PsiCIfPzpm2Zh5Dm-a1tRR3t-J9sfLv6P1aYAo/exec?name=John