Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Last active February 2, 2024 10:10
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcheng5/2aaff19e67079840350d08361fe7fb20 to your computer and use it in GitHub Desktop.
Save jcheng5/2aaff19e67079840350d08361fe7fb20 to your computer and use it in GitHub Desktop.
Accepting POST requests from Shiny

Accepting POST requests from Shiny

(This post was motivated by a talk by @jnolis at CascadiaRConf 2021)

Recent versions of Shiny have an undocumented feature for handling POST requests that are not associated with any specific Shiny session. (Note that this functionality is missing our normal level of polish; it's a fairly low-level hook that I needed to make some things possible, but doesn't make anything easy.)

In a nutshell, it works by replacing your traditional ui object with a function(req), and then marking that function with an attribute indicating that it knows how to handle both GET and POST:

library(shiny)

ui <- function(req) {
  # The `req` object is a Rook environment
  # See https://github.com/jeffreyhorner/Rook#the-environment
  if (identical(req$REQUEST_METHOD, "GET")) {
    fluidPage(
      # as normal...
    )
  } else if (identical(req$REQUEST_METHOD, "POST")) {
    # Handle the POST
    query_params <- parseQueryString(req$QUERY_STRING)
    body_bytes <- req$rook.input$read(-1)
    
    # Be sure to return a response
    httpResponse(
      status = 200L,
      content_type = "application/json",
      content = '{"status": "ok"}'
    )
  }
}
attr(ui, "http_methods_supported") <- c("GET", "POST")

server <- function(input, output, session) {
  # same as usual
}

shinyApp(ui, server)

Whether the request is a GET or POST, the response from the ui function can either be:

  • A normal Shiny UI object, which will be handled in the usual way
  • A shiny::httpResponse() object, which will be returned to the client verbatim(-ish)
  • NULL if the request is not applicable; Shiny will fall back to other logic, and ultimately return 404 if appropriate
  • Async is supported, you can return a promise that resolves to any of the above

Handling URL paths besides /

The example above works with GET or POST, but only on the root (/) URL path. If you want to handle GET and/or POST requests on other URLs, like /callback or whatever, the shinyApp function has a uiPattern argument that you can use to control exactly which URL paths should be routed to the ui function (for example, uiPattern = ".*"). The ui function can then look at req$PATH_INFO to see what the actual URL path was.

@rouuuge
Copy link

rouuuge commented Jun 6, 2023

hi,

is it somehow possible to start the session after a post request? I like to pass an access_token to the shiny-application. so far i have:

shiny:::handlerManager$addHandler(
  function(req) {
    if (identical(req$REQUEST_METHOD, "POST")) {
      body_bytes <- req$rook.input$read(-1)
      dataString <- rawToChar(body_bytes)
      access_token <- sub("^access_token=(.*)$", "\\1", dataString)
      print(access_token)
      
      return(httpResponse(
        status = 200L,
        content_type = "application/json",
        content = '{"status": "ok"}'
      ))
    }
  },
  "key"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment