[英]Is it possible to control the HTTP response behavior in openCPU?
According to the openCPU documentation , there are some default HTTP status codes and return types for a few situations.根据openCPU 文档,有一些默认的 HTTP 状态码和返回类型用于少数情况。 For example, when R raises an error, openCPU returns code
400
with a response type of text/plain
.例如,当 R 引发错误时,openCPU 返回代码
400
,响应类型为text/plain
。
While I believe it should be possible to control those things, is it possible to customize any of those things directly from R?虽然我相信应该可以控制这些东西,但是否可以直接从 R 自定义这些东西? For example, what if I wanted to return a JSON for a specific error in my R function, with a status code 503?
例如,如果我想针对我的 R function 中的特定错误返回 JSON,状态码为 503,该怎么办?
You can change their R package behavior by forking opencpu or via a local copy ie not sure if package allows this like a functionality but the responses are configured in res.R You can change their R package behavior by forking opencpu or via a local copy ie not sure if package allows this like a functionality but the responses are configured in res.R
For eg this method in the link above uses 400
for error.例如,上面链接中的这种方法使用
400
表示错误。
error <- function(msg, status=400){
setbody(msg);
finish(status);
}
I will update the answer if I can confirm this is available without changing package code.如果我可以在不更改 package 代码的情况下确认这是可用的,我将更新答案。
You can write your serving html ie index.html which uses opencpu.js
to call the corresponding R functions from your app, the return type can be requested to be json
in the opencpu.js
call. You can write your serving html ie index.html which uses
opencpu.js
to call the corresponding R functions from your app, the return type can be requested to be json
in the opencpu.js
call. And in the R function, you can tryCatch()
errors to send appropriate error code as a json argument.在 R function 中,您可以
tryCatch()
错误以将适当的错误代码作为 json 参数发送。
For eg in the stock example app you can see the file stock.js which calls the functions from R
folder ie例如,在股票示例应用程序中,您可以看到文件stock.js调用
R
文件夹中的函数,即
//this function gets a list of stocks to populate the tree panel
function loadtree(){
var req = ocpu.rpc("listbyindustry", {}, function(data){
Ext.getCmp("tree-panel").getStore().setProxy({
type : "memory",
data : data,
reader : {
type: "json"
}
});
Ext.getCmp("tree-panel").getStore().load();
}).fail(function(){
alert("Failed to load stocks: " + req.responseText);
});
}
The corresponding R code being called is in listbyindustry.R , inside which you can tryCatch()
and send custom json
.调用的相应 R 代码在listbyindustry.R中,您可以在其中
tryCatch()
并发送自定义json
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.