gpt4 book ai didi

http - http.ResponseWriter在主体中返回回车符

转载 作者:行者123 更新时间:2023-12-01 22:41:14 25 4
gpt4 key购买 nike

我正在使用Go的net/http库开发HTTP服务器的编码练习。该服务器应该在Gitlab管道中通过一系列测试。我无权访问这些测试,也看不到它们是如何实现的。
问题是,对预期的HTTP 204 No Content响应的一项测试失败,如下所示:

Expected an empty response body "", got "\n"
我在代码中构建响应的方式是:
// w is the http.ResponseWriter of the handler function.
w.WriteHeader(http.StatusNoContent)
w.Header().Del("Content-Type")
w.Write(nil)
我也尝试了 w.Write(make([]byte, 0))具有相同的结果。
我正在使用curl进行本地测试,但是我看不到正文中返回的字符:
$ curl -i --header "Content-Type: application/x-www-form-urlencoded" --request POST --data "PARAMETER=1" host:9000/path
HTTP/1.1 204 No Content
Date: Thu, 10 Sep 2020 16:12:21 GMT

$
net/http服务器是否实际上返回了回车符,如何防止这种情况?谢谢。

最佳答案

抱歉,我看错了一段代码。因为我没有有关测试的任何详细信息,所以我真的不知道要测试的确切案例。上面的注释是正确的,仅使用w.WriteHeader(http.StatusNoContent)不会在主体中产生任何回车符。无需删除Content-Type。我的错误是我改用http.Error(w, "", http.StatusNoContent)

关于http - http.ResponseWriter在主体中返回回车符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63833788/

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