我正在写一个简单的辛纳屈的应用程序,并给予用户发布与特定数据的请求,我想有消息CLIENT_ERROR,或类似的东西返回一个错误“453”(自定义错误代码)。
问题是:寻找到了西纳特拉文档,并做一些测试中,我无法找到一个方法来设置响应的错误消息,仅响应状态。
因此,如果设置西纳特拉响应
get '/' do
response.status = 453
end
我得到的错误代码的权利:
curl -v localhost:4567
* About to connect() to localhost port 4567 (#0)
* Trying 127.0.0.1... connected
> GET / HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:4567
> Accept: */*
>
< HTTP/1.1 453
< X-Frame-Options: sameorigin
< X-XSS-Protection: 1; mode=block
< Content-Type: text/html;charset=utf-8
< Content-Length: 0
< Connection: keep-alive
< Server: thin 1.3.1 codename Triple Espresso
<
* Connection #0 to host localhost left intact
* Closing connection #0
但我想有是:
< HTTP/1.1 453 CLIENT_ERROR
同样的方式,我有
< HTTP/1.1 200 OK
当一切根据计划进行。
反正有做到这一点使用西纳特拉/架?
通过正在使用的服务器,例如在薄产生的状态消息中的消息是在Thin::HTTP_STATUS_CODES
和产生效应初探线Thin::Response
,和的WEBrick它们在WEBrick::HHTPStatus::StatusMessage
和响应于所产生WEBrick::HTTPResponse
。
如果你知道你正在使用的服务器,你可以在你的错误添加到相应的哈希值。
薄:
require 'thin'
Thin::HTTP_STATUS_CODES[453] = "Client Error"
和输出:
$ curl -v localhost:4567
* About to connect() to localhost port 4567 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 4567 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3
> Host: localhost:4567
> Accept: */*
>
< HTTP/1.1 453 Client Error
< X-Frame-Options: sameorigin
< X-XSS-Protection: 1; mode=block
< Content-Type: text/html;charset=utf-8
< Content-Length: 0
< Connection: keep-alive
< Server: thin 1.4.1 codename Chromeo
<
* Connection #0 to host localhost left intact
* Closing connection #0
与使用WEBrick:
require 'webrick'
WEBrick::HTTPStatus::StatusMessage[453] = "Client Error"
其给出的输出:
$ curl -v localhost:4567
* About to connect() to localhost port 4567 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 4567 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3
> Host: localhost:4567
> Accept: */*
>
localhost - - [13/Aug/2012:01:41:48 BST] "GET / HTTP/1.1" 453 0
- -> /
< HTTP/1.1 453 Client Error
< X-Frame-Options: sameorigin
< X-Xss-Protection: 1; mode=block
< Content-Type: text/html;charset=utf-8
< Content-Length: 0
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
< Date: Mon, 13 Aug 2012 00:41:48 GMT
< Connection: Keep-Alive
<
* Connection #0 to host localhost left intact
* Closing connection #0
我会建议不要使用自定义HTTP状态代码。 如果你认为你有一般使用的东西,考虑写一个互联网草案,并通过IETF规范过程中去。