2

I am looking into adding the ability to handle POST requests to my Shiny app.

I read some posts on the topic over the web and stumbled upon this, this and this.

I thought this answer works well and I would like to build on it and allow my app server to observe the POST requests, such that after a request was posted the app would execute certain commands, something like:

# this code is from the linked answer
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(
      h1("Accepting POST requests from Shiny")
    )
  } else if (identical(req$REQUEST_METHOD, "POST")) {
    # Handle the POST
    query_params <- parseQueryString(req$QUERY_STRING)
    body_bytes <- req$rook.input$read(-1)
    if(req$PATH_INFO == "/iris"){
      postedIris <- jsonlite::fromJSON(rawToChar(body_bytes))
      modifiedIris <- postedIris[sapply(iris, class) == "numeric"]*as.numeric(query_params$factor)
      httpResponse(
        status = 200L,
        content_type = "application/json",
        content = jsonlite::toJSON(modifiedIris, dataframe = "columns")
      )
    } else {
      httpResponse(
        status = 200L,
        content_type = "application/json",
        content = '{"status": "ok"}'
      )
    }
  }
}
attr(ui, "http_methods_supported") <- c("GET", "POST")

server <- function(input, output, session){
    observeEvent(something_here, {
            # do something after a request was made/processed
        }) 
}

Is this possible? If so, how can I do it? Thanks in advance!

aaa
  • 63
  • 3
  • did you solve this? i have a similar problem: https://gist.github.com/jcheng5/2aaff19e67079840350d08361fe7fb20 – rouge Jun 06 '23 at 12:35
  • @rouge Sadly, no, I could not find a solution. I played around with `session$registerDataObj` but that has a problem of having an unique api route for each session. I thought about setting up a two way street, sending the unique api route to whatever app is making the post request and then using it on said app, but never really got to implement it. Best of luck though! This topic is not documented much. – aaa Jun 07 '23 at 13:30

0 Answers0