如何从RestRserve中的路径提取参数?

时间:2020-08-02 06:56:14

标签: r api restrserve

这是应用程序的代码

# load_libraries ----------------------------

library("RestRserve")

# func_UserId ----------------------------

get_userid <- function(req, res) {
  id = req$get_param_path("id")
  res$set_body(id)
}

# create app --------------------------------

app = Application$new()

# define Routes -----------------------------

### /user/:id main Route
app$add_get(
  path = "/user/{id}",
  FUN = get_userid,
  match = "exact"
)

# Run App -----------------------------------

backend = BackendRserve$new()
backend$start(app, http_port = 9000)

,我无法从路线中提取ID。我阅读了文档,但还不够清楚。如果您有任何答案,请告诉我。

预先感谢

1 个答案:

答案 0 :(得分:2)

library(RestRserve)
app = Application$new()
app$add_get("/user/{id}", function(req, res) {
  str(req$parameters_path)
}, match = "regex")

req = Request$new("/user/100500", method = "GET")
res = app$process_request(req)
#List of 1
# $ id: chr "100500"