简体   繁体   English

如何从 OpenCPU R 函数内部设置自定义 HTTP 状态代码

[英]How to set a custom HTTP status code from inside OpenCPU R function

I have an R function on OpenCPU (provided from OpenCPU docker image: https://hub.docker.com/r/opencpu/rstudio/ ) which filters data by some criterias.我在 OpenCPU 上有一个 R 函数(由 OpenCPU docker 图像提供: https ://hub.docker.com/r/opencpu/rstudio/),它按某些标准过滤数据。 If there is no data to return I want to send a message (No data) with 204 status code.如果没有要返回的数据,我想发送带有 204 状态代码的消息(无数据)。 How can I set status status code from inside the R function?如何从 R 函数内部设置状态代码?

There is information in rApache documentation ( http://rapache.net/manual.html ). rApache 文档中有信息 ( http://rapache.net/manual.html )。 It is said there is a function setStatus() in rApache, but I couldn't set status code when calling this function.据说rApache中有一个函数setStatus(),但是我在调​​用这个函数时无法设置状态码。 I tried to set 204 status code, but I get a default (for POST) 201 status code instead.我试图设置 204 状态代码,但我得到了一个默认的(对于 POST)201 状态代码。

filterData <- function(filter_a = NULL, filter_b = NULL) {

  data <- as.data.frame(
    list("a" = c(1,2,3), "b" = c(3,2,1))
  )

  if (!is.null(filter_a)) {
    data <- subset(data, a == filter_a)
  }

  if (!is.null(filter_b)) {
    data <- subset(data, b == filter_b)
  }

  if (nrow(data) == 0) {
    setStatus(204L)
    return("No content")
  } else {
    return(data)
  }
}

The short answer: this is not possible.简短的回答:这是不可能的。

The logic for setting the response status codes is encapsulated within openCPU.设置响应状态码的逻辑封装在openCPU中。 Status codes are managed within the res object.状态代码在res对象中进行管理。 In case of POST requests openCPU will overwrite the status code in session_eval even if you would manage to set the status code in the internal object.在 POST 请求的情况下,即使您设法在内部对象中设置状态代码,openCPU 也会覆盖session_eval 中的状态代码。 OpenCPU would overwrite it with 201 Code after executing your custom code.执行您的自定义代码后,OpenCPU 会用 201 代码覆盖它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM