http - 有什么方法可以自定义 HTTP 响应状态?

标签 http go web

我需要在我的 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/

相关文章:

http - 使用 groovy 和数据发送 POST 已经进行了 URL 编码

我可以在 C 程序中使用 GET 方法吗

http - 在 cpp-netlib 中添加 Access-Control-Allow-Origin 选项

sorting - 排序时忽略 nil 元素

带有移动和 Web 客户端的 Amazon Web Services 上的 Java 应用程序

java - 通过 ReSTLet 删除带有有效负载的请求

Golang 在下载时停止导航

json - 如何从 golang 中的 json 访问键和值?

css - 添加其他 div 时,标题 div 会更改其位置

email - 通过 sendgrid 在电子邮件中添加 "web version link"