plumber icon indicating copy to clipboard operation
plumber copied to clipboard

Improved documentation for `forward()`

Open JosiahParry opened this issue 2 years ago • 5 comments

https://github.com/rstudio/plumber/blob/06e46f3ff5119e5f1cb8af29ef49aecb3cbb932a/R/plumber-step.R#L3

The documentation for forward() is rather slim. Upon reading the function documentation it is unclear how the function is to be used.

For more about this function I navigated to the article "Routing and Input" which states "...then calls forward() to pass control to the next handler in the pipeline (another filter or an endpoint)" which makes me believe that you can use foward() to pass to another endpoint.

The example provides no clear description as to what is being forwarded and to where. Further all examples of forward() are within the context of filter.

Further documentation or examples would be helpful.

There's also the possibility that this is a uniquely me problem.

JosiahParry avatar Sep 30 '21 21:09 JosiahParry

@JosiahParry nah. I'm with you on forward(). It's an us problem :)

meztez avatar Oct 01 '21 02:10 meztez

I agree it is confusing. I have been leaving that alone as I don't like the design (and therefore don't want to promote it with docs). I hope sooner than later we can get middleware and just silently ignore filters/forward() forever. (I don't have enough time to implement this in the next 6 months)

Few notes to hopefully clear things up:

  • forward() can be called at anytime during a filter. (I'd prefer it to be the last thing returned if the next filter should be processed.)
  • If forward() is called during the current filter execution, the next filter in the router will be executed. If there are no more filters left, then the endpoint will be processed.

schloerke avatar Oct 01 '21 03:10 schloerke

Thanks, Barret! So to be clear, forward() does not enable the passing to another endpoint? Say from a get to a post?

JosiahParry avatar Oct 01 '21 03:10 JosiahParry

Correct. It does not enable passing the route.

It is more of "if forward() is not called within the filter, then the return value of the filter is used to respond to the request"

schloerke avatar Oct 01 '21 03:10 schloerke

To answer your specific question...

Say from a get to a post?

By the time the filter is being executed, httpuv / rook have already processed all of the headers and information. There would be no body to parse even if you changed the information in the req object for follow up filters to use

schloerke avatar Oct 01 '21 03:10 schloerke