Do you use a routers for doGet, doPost?

1,184 views
Skip to first unread message

Alex

unread,
Jan 23, 2020, 12:22:24 AM1/23/20
to google-apps-sc...@googlegroups.com
Hi!

Do you use a router for doGet, doPost? Which one?

Can you share a public repo of that?

Best, Alex.

Alan Wells

unread,
Jan 23, 2020, 8:51:13 AM1/23/20
to Google Apps Script Community
Do you mean having the code do something different depending upon some value being passed to it?

Dimu Designs

unread,
Jan 23, 2020, 9:00:29 AM1/23/20
to Google Apps Script Community
There is an undocumented feature where you can add custom paths to a web app url for routing (and you can reference that path via the 'pathInfo' property on the event object). But you'll have to pass a user access token with the request (for both GET and POST) regardless of the settings used to deploy the web app. 

Alan Wells

unread,
Jan 23, 2020, 9:21:42 AM1/23/20
to Google Apps Script Community
How is the access token processed in the Apps Script Web App?


Thursday, January 23, 2020  Dimu Designs wrote:
There is an undocumented feature where you can add custom paths to a web app url for routing (and you can reference that path via the 'pathInfo' property on the event object). But you'll have to pass a user access token with the request (for both GET and POST) regardless of the settings used to deploy the web app. 

Thursday, January 23, 2020 Alex wrote:

Dimu Designs

unread,
Jan 23, 2020, 10:27:03 AM1/23/20
to google-apps-sc...@googlegroups.com
You can pass it as a url variable ("access_token=[ACCESS_TOKEN_VALUE]") or in the Authorization header as a Bearer token when making requests to the web url (and if I recall correctly, you may need to have the Drive API enabled as well). I'm not sure, but the last time I played with it, it seemed that only the owner of the script could access routing this way, so its probably not something you can scale to multiple users. Maybe that's why its undocumented...

Alan Wells

unread,
Jan 23, 2020, 10:43:00 AM1/23/20
to Google Apps Script Community
Okay, I guess the reason to pass an Access Token, would be to run a service on behalf of the user, to access the users account, even though the project is owned by the developer? 

I'm not sure what Alex meant by "use a router?"

Thursday, January 23 Dimu Designs wrote:
You can pass it as a url variable ("access_token=[ACCESS_TOKEN_VALUE]") when making requests to the web url or in the Authorization header as a Bearer token. I'm not sure, but the last time I played with it, it seemed that only the owner of the script could access routing this way, so its probably not something you can scale to multiple users.

David Gentile

unread,
Jan 23, 2020, 3:47:53 PM1/23/20
to Google Apps Script Community
Have a watch of this video: https://www.youtube.com/watch?v=9LHPU0dYyrU

Typically you're using a router for doGet and redirecting a request to a specific page.

In my projects it would look like "<url>?v=view_order&id=123456"

This will load the "order" html file, and also pass the "id" value which is used to load dynamic content.


My doGet function looks like this

function doGet(e) {
  
  var parameters = e.parameters;
  
  return Router(parameters);
  
}


Then my Router function looks like this
function Router(parameters) {
  var view, id;

  view = 'v' in parameters ? parameters.v[0] : null ;
  id = 'id' in parameters ? parameters.id[0] : null;

  switch (view) {
    case 'orders':
      return loadOrders();
    
    case 'view_order':
      return loadOrder(id);
   
    default:
      return loadOrders();
  }
}

function loadOrders() {

  var orderData = getUpcomingOrders();
  var orders = orderData.orders;

  return render("Views/Pages/Orders/index", {
    orders: orders
  });

}

function loadOrder(id) {

  var orderData = getOrder(id);
  var order = createOrderObj(orderData);

  return render("Views/Pages/View_Order/index", {
    order: order,
  });
}

Finally I have a "render" function which loads the html template and passes in any template arguments
function render(file, argsObject){
  var html = HtmlService.createTemplateFromFile(file);
 
  if(argsObject) {
    var keys = Object.keys(argsObject);
    keys.forEach(function(key){
      html[key] = argsObject[key];
    });
  }
  
  return html.evaluate().setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}


Hope that's helpful!

Alex

unread,
Jan 23, 2020, 9:41:57 PM1/23/20
to google-apps-sc...@googlegroups.com

Do you mean having the code do something different depending upon some value being passed to it?

Yes I do. I mean do you use ready-made modules of GAS or pure JS for that?

There is an undocumented feature where you can add custom paths to a web app url for routing

Yep. It seems this feature may be helpful

I'm not sure what Alex meant by "use a router?"

 @aj I practice to in English. Im very glad that you understand me at least a little. Thanks!
Exactly! I'm using a dictionary pattern as a router too. But sometimes I need something more advanced. For an example I want to control what i have into var contents = JSON.parse(e.postData.contents);
and I'd like omit or rise exceptions via chaining. Something like this would be cool

function doPost(e){
 
var router = new Route(e);
  router
.matches(); // <== the rules for checkers
 
return router
   
.on(checker, callback)
   
.on(checker, callback)
   
.resolve();
}

Hope it's clean!

Clark Lind

unread,
Jan 24, 2020, 9:21:50 PM1/24/20
to Google Apps Script Community
Ashton Fey did something similar which may give you some ideas. (https://youtu.be/xMIYGXLOBcM). His code is linked here: https://script.google.com/d/161193BTweCReKhgKMOyKZoq3GA3TuK3WJCnxVrJtElwLU0Syg_oH6PkU/edit?usp=drive_open

Alex

unread,
Jan 25, 2020, 10:47:51 AM1/25/20
to Google Apps Script Community
OK. Thanks!
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Ali Nabavi

unread,
Feb 2, 2023, 10:17:11 PM2/2/23
to Google Apps Script Community
Thank you!  :-)

Silly question: why is "Router" capitalized there?

-Ali

Maksym Stoianov

unread,
Sep 2, 2025, 7:11:37 AM (5 days ago) Sep 2
to Google Apps Script Community

Hi Alex,

I've been working on a library called Boot for Google Apps Script Projects that makes this process much cleaner and more scalable.

To use this library, you'll need a modern development workflow: it's designed to work in a local development environment with clasp and a project bundler. It also has full TypeScript support.

It uses decorators to simplify REST API routing. Here's a quick example of how you can set up a controller and a GET endpoint:

Define a REST controller that will handle HTTP requests to your Apps Script web application:

import {Get, RestController} from "appsscript-boot";

@RestController("api/sheet")
export class Sheet {

  @Get("active-range")
  getActiveRange(): string {
    return "This action return active range.";
  }
}


This code creates a REST controller at /api/sheet and defines a GET endpoint at /active-range. It shows how you can easily structure your web app without complex manual routing.

You can find the full library and documentation on GitHub:
https://github.com/MaksymStoianov/appsscript-boot

I hope this helps! I'd love to hear your thoughts on it.

Best regards,
Max


четверг, 23 января 2020 г. в 06:22:24 UTC+1, Alex:

Tim Dobson

unread,
Sep 2, 2025, 4:01:03 PM (5 days ago) Sep 2
to google-apps-sc...@googlegroups.com
This looks neat Max.

Thanks for sharing - I'll have a play with it.

-Tim

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/77180ee5-63e3-4c7e-9a71-08d9fdb27835n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages