如何收集脚本的HTTP响应状态?下面是一个示例代码,它将启动一个服务器并允许两个交互路由。
using Genie
import Genie.Router: route
import Genie.Renderer.Json: json
Genie.config.run_as_server = true
route("/try/", method=GET) do
(:message => "Welcome") |> json
end
route("/test/", method=POST) do
data = jsonpayload()
<body>
end
Genie.startup()如何将200、500等响应状态作为字符串变量进行收集?
发布于 2021-06-25 19:34:08
使用HTTP打开与服务器的连接,并查找status字段:
julia> using HTTP
julia> response = HTTP.get("http://127.0.0.1:8000/try")
HTTP.Messages.Response:
"""
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Genie/1.18.1/Julia/1.6.1
Transfer-Encoding: chunked
{"message":"Welcome"}"""
julia> response.status
200如果你想自己控制状态,你可以在服务器端添加:
route("/tryerror/", method=GET) do
Genie.Responses.setstatus(503)
end现在让我们测试一下503
julia> response = HTTP.get("http://127.0.0.1:8000/tryerror")
ERROR: HTTP.ExceptionRequest.StatusError(503, "GET", "/tryerror", HTTP.Messages.Response:
"""
HTTP/1.1 503 Service Unavailable
Content-Type:
Server: Genie/1.18.1/Julia/1.6.1
Transfer-Encoding: chunked
""")https://stackoverflow.com/questions/68127425
复制相似问题