Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return uri to redirect to #418

Closed
yonicd opened this issue Apr 29, 2019 · 8 comments
Closed

return uri to redirect to #418

yonicd opened this issue Apr 29, 2019 · 8 comments
Labels
answered? Waiting for confirmation that the issue is solved type: question Poses a question, not a problem

Comments

@yonicd
Copy link

yonicd commented Apr 29, 2019

how do i build a return from a get so based on the input a new url is created and as a result a redirect to the uri is invoked? i can return the uri as a string via @html.

@yonicd yonicd changed the title return url to redirect to return uri to redirect to Apr 29, 2019
@schloerke
Copy link
Collaborator

If you visit 127.0.0.1:12345/ it will redirect to 127.0.0.1:12345/redirected with the value "worked!". Please see https://en.wikipedia.org/wiki/URL_redirection#HTTP_status_codes_3xx to get the correct status code (permanent vs temp).

# plumber.R
#' @get /redirected
function(req, res) {
  "worked!"
}

#' @get /
#' @html
function(req, res) {
  res$status <- 303 # redirect
  res$setHeader("Location", "./redirected")
"<html>
  <head>
    <meta http-equiv=\"Refresh\" content=\"0; url=./redirected\" />
  </head>
  <body>
    <p>Please follow <a href=\"http://www.example.com/\">this link</a>.</p>
  </body>
</html>"
}

Server

plumber::plumb("plumber.R")$run(port = 12345)

Testing

# bash

curl http://127.0.0.1:12345/
# <html>
#   <head>
#     <meta http-equiv="Refresh" content="0; url=./redirected" />
#   </head>
#   <body>
#     <p>Please follow <a href="http://www.example.com/">this link</a>.</p>
#   </body>
# </html>

# Follow redirects using curl
curl http://127.0.0.1:12345/ -L
# ["worked!"]

@schloerke schloerke added answered? Waiting for confirmation that the issue is solved type: question Poses a question, not a problem labels Apr 29, 2019
@yonicd
Copy link
Author

yonicd commented Apr 29, 2019

Thanks! I’ll try this out.

@yonicd
Copy link
Author

yonicd commented Apr 30, 2019

how would it work in this case that I want to redirect to a site outside of my server?

#' @html
function(req, res) {
  res$status <- 303 # redirect
  res$setHeader("Location", "https://github.com")
  "<html>
  <head>
    <meta http-equiv=\"Refresh\" content=\"0; url=https://github.com\" />
  </head>
  <body>
    <p>Please follow <a href=\"http://www.example.com/\">this link</a>.</p>
  </body>
</html>"
}```

@schloerke
Copy link
Collaborator

Yup. That should work!

The header is for applications like curl. The meta tag within the html head is for browsers. (I forgot to change example.com as well.)

@yonicd
Copy link
Author

yonicd commented Apr 30, 2019

looks like something isn't quite right still.

Screen Shot 2019-04-30 at 9 27 40 AM

@yonicd
Copy link
Author

yonicd commented Apr 30, 2019

ah the swagger returns an error, but the app redirects correctly.

@schloerke
Copy link
Collaborator

Ah. The response type is set to application/json. It needs to be application/html.

@schloerke
Copy link
Collaborator

Closing for now. I'll make an issue for getting content type / swagger info from serializers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered? Waiting for confirmation that the issue is solved type: question Poses a question, not a problem
Projects
None yet
Development

No branches or pull requests

2 participants