我需要在我的 Web 服务器的处理程序中自定义 HTTP 状态,但我发现 golang 似乎不支持完全自定义的 HTTP 响应。 例如:
w.WriteHeader(999)
这是 curl
结果:
$ curl -vk -i localhost:9898/hello
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 9898 (#0)
> GET /hello HTTP/1.1
> Host: localhost:9898
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 999 status code 999
HTTP/1.1 999 status code 999
< Date: Wed, 19 Jun 2019 02:01:09 GMT
Date: Wed, 19 Jun 2019 02:01:09 GMT
< Content-Length: 0
Content-Length: 0
结果中的status code
似乎无法修改..但是,我想定义自己的状态,比如Bad Request: xxx
。
有什么好主意吗?非常感谢!
最佳答案
我认为目前不支持您尝试的内容。
根据 the official documentation of net/http.ResponseWriter.WriteHeader
:
The provided code must be a valid HTTP 1xx-5xx status code. Only one header may be written. Go does not currently support sending user-defined 1xx informational headers, with the exception of 100-continue response header that the Server sends automatically when the Request.Body is read.
关于http - 有什么方法可以自定义 HTTP 响应状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56658947/