如何使用 Luracast Restler 返回除 HTTP 200 OK 之外的肯定响应?

发布于 2024-12-06 17:39:38 字数 196 浏览 1 评论 0原文

我知道 Restler 自动返回我的方法的结果,HTTP 状态代码为 200(正常),如果我想返回错误响应,我可以使用

throw new RestException(400); // returns HTTP 400 Bad Request

但是如何返回 HTTP 201 Created 的响应以及结果值?

I understand that Restler automatically returns the result of my method with the HTTP Status code 200 (OK) and if I want to return an error response I use

throw new RestException(400); // returns HTTP 400 Bad Request

But how do I return a response of say, HTTP 201 Created along with the resulting value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赴月观长安 2024-12-13 17:39:38

我也点击了这个,我手动返回成功状态:

return(array('success' => array('code' => 201, 'msg' => 'resulting value')));

如果您希望 RESTler 为您处理它:

throw new RestException(201, 'resulting value');

但这将在“错误”状态下返回:

{
    "error": {
        "code": 201,
        "msg": "resulting value"
    }
}

I hit this too and I'm returning success states manually:

return(array('success' => array('code' => 201, 'msg' => 'resulting value')));

If you want RESTler to handle it for you:

throw new RestException(201, 'resulting value');

but that will be returned under the "error" state:

{
    "error": {
        "code": 201,
        "msg": "resulting value"
    }
}
晨光如昨 2024-12-13 17:39:38

从 RESTLER 3 开始,您可以在 API 方法上方的文档注释中设置备用肯定响应代码。例如,下面的文档注释设置了返回状态为 204 的 DELETE 路由(只要不抛出 RESTException)

/**
 * Delete an Attribute
 * @status 204
 * @url DELETE {eventTicketId}/registration/{eventTicketRegistrationId}/attribute/{attributeId}
 */

function deleteTicketRegistrationAttribute($eventTicketId,$eventTicketRegistrationId,$attributeId)
{

}

As of RESTLER 3, you can set an alternate positive response code in the Doc Comments above your API Method. For example, the below Doc Comment sets a DELETE route with a return status of 204 (as long as you don't throw a RESTException)

/**
 * Delete an Attribute
 * @status 204
 * @url DELETE {eventTicketId}/registration/{eventTicketRegistrationId}/attribute/{attributeId}
 */

function deleteTicketRegistrationAttribute($eventTicketId,$eventTicketRegistrationId,$attributeId)
{

}
ペ泪落弦音 2024-12-13 17:39:38
header("HTTP/1.1 201 Some response");

应该有效

header("HTTP/1.1 201 Some response");

should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文