Plumber: How to redirect from root to __docs__

Created on 30 Sep 2020  路  9Comments  路  Source: rstudio/plumber

Hi,

I'm currently working on an API (using plumber) inside a docker container and I would like to redirect to the __docs__ endpoint whenever the root endpoint is called. (e.g. redirect from https://localhost:8000/ to https://localhost:8000/__docs__)

Can you please tell me how it is possible to achieve this behavior using plumber?

Thank you very much in advance :)

knowledge question

Most helpful comment

You can do an http server rewrite with apache or nginx. I would ask over at RStudio Community. You could probably do it from plumber too using.

library(plumber)

#* @get /hello
function() {
  "Is it me you are looking for?"
}

#* @preempt __first__
#* @get /
function(req, res) {
  res$status <- 301 # redirect permanentl"y, or other 300 range http status code
  res$setHeader("Location", "./__docs__/")
  res$body <- "redirecting..."
  res
}

# If you want to remove / from API spec
#* @plumber
function(pr) {
  pr_set_api_spec(pr, function(spec) {
    spec$paths$`/` <- NULL
    spec
  })
}

Ask a Question
The issue tracker is not for general help questions -- please ask general plumber help questions on RStudio Community, https://community.rstudio.com/tag/plumber.

All 9 comments

You can do an http server rewrite with apache or nginx. I would ask over at RStudio Community. You could probably do it from plumber too using.

library(plumber)

#* @get /hello
function() {
  "Is it me you are looking for?"
}

#* @preempt __first__
#* @get /
function(req, res) {
  res$status <- 301 # redirect permanentl"y, or other 300 range http status code
  res$setHeader("Location", "./__docs__/")
  res$body <- "redirecting..."
  res
}

# If you want to remove / from API spec
#* @plumber
function(pr) {
  pr_set_api_spec(pr, function(spec) {
    spec$paths$`/` <- NULL
    spec
  })
}

Ask a Question
The issue tracker is not for general help questions -- please ask general plumber help questions on RStudio Community, https://community.rstudio.com/tag/plumber.

Note, this only works if forward = FALSE I believe in deployment.

Not a redirect, but I just used:

#* @get /
#* @serializer html
function(){
  '<html><body><h1>Please visit the <a href="./__docs__/">docs page</a> for documentation</h1></body></html>'
}

for a landing page.

I would add a redirect meta tag into the head of the html to auto redirect web browsers

Ex:

<html>
    <head>
        <meta http-equiv="refresh" content="0; url=./__docs__/" />
    </head>
    <body><h1>If not redirected, please visit the <a href="./__docs__/">docs page</a> for documentation</h1></body>
</html>

Agree - that makes it better. What I meant by forwarding is that if you have forwarding on an just go to IPADDRESS, it'll show you IPADDRESS/APPLICATION, but when you use the redirect like that, it'll go to IPADDRESS/__docs__ not IPADDRESS/APPLICATION/__docs__. If you go to IPADDRESS/APPLICATION though, I think it's fine.

BTW - I think this only really happens in deployment (particularly with the configuration for DO), not a local build.

@muschellij2 Ah, correct. I've updated https://github.com/rstudio/plumber/issues/682#issuecomment-701361493 above to redirect to ./__docs__/ and not ../__docs__/

We have the same redirect in the package for /__swagger__/, could that be an issue?

https://github.com/rstudio/plumber/blob/43a7125113b05c5ff4cd84260ea95ff165d8bcd3/R/ui.R#L265

We have the same redirect in the package for /swagger/, could that be an issue?

@meztez No. The trailing slash in /__swagger__/ makes it act as if it's two layers (similar to /__swagger__/index.html). So if we're at that route, we'd like to go up one layer ('..') and into /__docs__/.

In @DODA-XXXL's situation, they are starting at the top level (/ or even /index.html).

Thank you very much! The solution from @meztez works like a charm :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dpmccabe picture dpmccabe  路  6Comments

alexzaytsev-newsroomly picture alexzaytsev-newsroomly  路  6Comments

trestletech picture trestletech  路  5Comments

utkarshsaraf19 picture utkarshsaraf19  路  3Comments

atheriel picture atheriel  路  4Comments