有什么方法可以自定义Golang中的HTTP响应状态?

Eri*_*han -1 http go web

我需要在Web服务器的处理程序中自定义HTTP状态,但是我发现golang似乎不支持完全自定义的HTTP响应。例如:

w.WriteHeader(999)
Run Code Online (Sandbox Code Playgroud)

这里是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
Run Code Online (Sandbox Code Playgroud)

status code在结果似乎不能被修改..但是,我想确定自己的地位,像Bad Request: xxx

有什么好主意吗?非常感谢!

Adi*_*ada 6

我认为目前不支持您尝试的内容。

根据以下官方文件net/http.ResponseWriter.WriteHeader

提供的代码必须是有效的HTTP 1xx-5xx状态代码。只能写入一个标题。Go当前不支持发送用户定义的1xx信息性标头,但服务器会在读取Request.Body时自动发送的100个继续响应标头,但不支持发送该标头。