gpt4 book ai didi

go - 如何避免发送 Content-Length header

转载 作者:IT王子 更新时间:2023-10-29 00:40:39 33 4
gpt4 key购买 nike

对于流式 http 端点,有没有办法避免发送长度?

  w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Del("Content-Length")

这就是我得到的。

HTTP/1.1 200 OK
Content-Length: 0
Content-Type: image/jpeg
Date: Mon, 23 Jun 2014 10:00:59 GMT
Transfer-Encoding: chunked
Transfer-Encoding: chunked

服务器也会打印一条警告。

2014/06/23 06:04:03 http: WriteHeader called with both Transfer-Encoding of "chunked" and a Content-Length of 0

最佳答案

您不应手动设置Transfer-Encoding。 Go 会为您做这件事,还有 Content-Length

curl,Go http 客户端或任何标准的 http 客户端将自动正确读取分 block 或非分 block 的 http 响应。

分 block 服务器的小例子:http://play.golang.org/p/miEV7URi8P

package main

import (
"io"
"log"
"net/http"
)

// hello world, the web server
func HelloServer(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
for i := 0; i < 5; i++ {
io.WriteString(w, "hello, world!\n")
w.(http.Flusher).Flush()
}
}

func main() {
http.HandleFunc("/", HelloServer)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

在图像/jpeg 的情况下,您可以将分 block 决策委托(delegate)给 Go 或手动从图像发送 N 个字节然后刷新。

关于go - 如何避免发送 Content-Length header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24363240/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com