为了账号安全,请及时绑定邮箱和手机立即绑定

在分块数据的 HTTP 响应中如何设置 Content-Length

在分块数据的 HTTP 响应中如何设置 Content-Length

Go
尚方宝剑之说 2023-06-19 14:15:19
我们编写了一个服务,它将一些编码数据分块发送到需要设置 Content-Length 标头的代理服务,以便它可以向端点发送正确的响应。即使我设置了 Content-Length 标头,它仍然会作为对客户端的响应的一部分被剥离。下面是设置标题的代码func HTTPSuccessResponse(rw http.ResponseWriter, bufferLen int, media []byte) {        rw.WriteHeader(http.StatusOK)        rw.Header().Set("Content-Type", "opus/ogg; audio/ogg; codec=opus")        length := strconv.Itoa(len(media));        rw.Header().Set("Content-Length", length)        rw.Write(media)}下面是我尝试使用 curl 请求时得到的响应bash-4.2# curl -v -X GET -k -H  -i 'http://127.0.0.1:8090/preview'* About to connect() to 127.0.0.1 port 8090 (#0)*   Trying 127.0.0.1...* Connected to 127.0.0.1 (127.0.0.1) port 8090 (#0)> GET /preview HTTP/1.1> User-Agent: curl/7.29.0> Host: 127.0.0.1:8090> Accept: */*>< HTTP/1.1 200 OK< Date: Tue, 14 May 2019 13:08:20 GMT< Content-Type: text/plain; charset=utf-8< Transfer-Encoding: chunked<我正在使用 Gorrila Mux 库来设置 HTTP 服务器。关于如何将标头作为响应的一部分的任何想法。
查看完整描述

1 回答

?
www说

TA贡献1775条经验 获得超8个赞

删除WriteHeader顶部的呼叫。您只能将标头写入响应一次。通话后,WriteHeader您无法再设置任何标题。

根据 ResponseWriter 文档:

    // Changing the header map after a call to WriteHeader (or

    // Write) has no effect unless the modified headers are

    // trailers.

所以你不能先调用它;但您也根本不需要调用它 - 来自相同的文档:


    // If WriteHeader is not called explicitly, the first call to Write

    // will trigger an implicit WriteHeader(http.StatusOK).

    // Thus explicit calls to WriteHeader are mainly used to

    // send error codes.


查看完整回答
反对 回复 2023-06-19
  • 1 回答
  • 0 关注
  • 59 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信