【问题标题】:In HTTP response for a chunked data how to set Content-Length在分块数据的 HTTP 响应中如何设置 Content-Length
【发布时间】:2019-05-14 13:31:41
【问题描述】:

我们编写了一个服务,它将一些编码数据作为分块发送到需要设置 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 服务器。任何想法如何将标题作为响应的一部分。

【问题讨论】:

  • 在已经写入后设置标题无效。要么删除 WriteHeader 调用(如果用 200 调用它是多余的),或者在设置所有标题后调用 WriteHeader。
  • Content-Length 和分块编码是互斥的。你不能两者都做。
  • @Peter:不完全正确......它可以通过设置页脚产生效果。

标签: go mux


【解决方案1】:

删除顶部的WriteHeader 调用。您只能将标头写入响应一次。致电WriteHeader 后,您将无法再设置任何标头。

Per the ResponseWriter documentation:

    // 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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多