gpt4 book ai didi

http.ResponseWriter 不设置标题内容类型

转载 作者:数据小太阳 更新时间:2023-10-29 03:39:12 26 4
gpt4 key购买 nike

我从 Brian W. Kernighan 和 Alan Donovan 的书 The Go Programming Language 中编写了任务。这是任务 № 3.4我的请求处理程序如下所示:

func handler(w http.ResponseWriter, r *http.Request) {
poly(w)
w.Header().Set("ContentType", "image/svg+xml")
fmt.Println(w.Header().Get("ContentType"))
}

poly(w) - 它是在 Writer 中返回 svg 文件的函数。另外,我检查了 ContentType 的值,它是“image/svg+xml”。但是当我在 chrome(F12) 中查看开发菜单时,我看到了这个: network menu in debug

当然,我看到的是 svn 文件的 xml 文本,而不是图片。

所以,我有疑问:这是我的错误,或者是 golang 中的错误,或者是正常的 sutiation。

最佳答案

您必须在编写响应正文之前设置 header 。查看ResponseWriter文档以获取更多详细信息。

还有一个打字错误。 header 名称是“Content-Type”,而不是“ContentType”

func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/svg+xml")
poly(w)
}

关于http.ResponseWriter 不设置标题内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41489300/

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