解决Golang中ResponseWriter的一个坑


Posted in Golang onApril 27, 2021

在使用Context.ResponseWriter中的Set/WriteHeader/Write这三个方法时,使用顺序必须如下所示,否则会出现某一设置不生效的情况。

ctx.ResponseWriter.Header().Set("Content-type", "application/text")
 ctx.ResponseWriter.WriteHeader(403)
 ctx.ResponseWriter.Write([]byte(resp))

如1:

ctx.ResponseWriter.Header().Set("Content-type", "application/text")
 ctx.ResponseWriter.Write([]byte(resp))
 ctx.ResponseWriter.WriteHeader(403)

会导致返回码一直是200

补充:Go里w http.ResponseWriter,调用w.Write()方法报错

Go里w http.ResponseWriter写入报错

http: request method or response status code does not allow

1. 下面是报错截图

解决Golang中ResponseWriter的一个坑

2. 点进去Write方法

它首先是一个接口;

由于它是在HTTP web服务器的应用场景,所以它具体的实现方法在net/http/server.go里:

func (w *response) Write(data []byte) (n int, err error) {
 return w.write(len(data), data, "")
}

再点进去,函数里你会发现有一个关键的判断

// 其中ErrBodyNotAllowed的
// 代码内容
// ErrBodyNotAllowed = errors.New("http: request method or response status code does not allow body")
if !w.bodyAllowed() {
 return 0, ErrBodyNotAllowed
}

点进去,发现它在没有设置Header时会panic,当然这跟我们当前要讨论的问题关系不大,关键在bodyAllowedForStatus()方法

func (w *response) bodyAllowed() bool {
 if !w.wroteHeader {
  panic("")
 }
 return bodyAllowedForStatus(w.status)
}

再点,终于看到了,当设置状态码为【100,199】、204、304就会报这个错,而我刚好设置的状态码就是204,我把它改成200重新试下,问题解决。

func bodyAllowedForStatus(status int) bool {
 switch {
 case status >= 100 && status <= 199:
  return false
 case status == 204:
  return false
 case status == 304:
  return false
 }
 return true
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持三水点靠木。如有错误或未考虑完全的地方,望不吝赐教。

Golang 相关文章推荐
为什么不建议在go项目中使用init()
Apr 12 Golang
go结构体嵌套的切片数组操作
Apr 28 Golang
解决golang在import自己的包报错的问题
Apr 29 Golang
golang DNS服务器的简单实现操作
Apr 30 Golang
解决golang post文件时Content-Type出现的问题
May 02 Golang
go语言中GOPATH GOROOT的作用和设置方式
May 05 Golang
Golang之sync.Pool使用详解
May 06 Golang
goland 设置project gopath的操作
May 06 Golang
浅谈GO中的Channel以及死锁的造成
Mar 18 Golang
golang为什么要统一错误处理
Apr 03 Golang
Golang ort 中的sortInts 方法
Apr 24 Golang
Go结合Gin导出Mysql数据到Excel表格
Aug 05 Golang
golang在GRPC中设置client的超时时间
golang http使用踩过的坑与填坑指南
Apr 27 #Golang
Golang 实现超大文件读取的两种方法
Apr 27 #Golang
golang中的空slice案例
Apr 27 #Golang
Go语言切片前或中间插入项与内置copy()函数详解
golang中切片copy复制和等号复制的区别介绍
Apr 27 #Golang
go语言中切片与内存复制 memcpy 的实现操作
Apr 27 #Golang
You might like
微信公众平台天气预报功能开发
2014/07/06 PHP
extjs中grid中嵌入动态combobox的应用
2011/01/01 Javascript
javascript实现控制div颜色
2015/07/07 Javascript
浅析jQuery Mobile的初始化事件
2015/12/03 Javascript
深入理解关于javascript中apply()和call()方法的区别
2016/04/12 Javascript
Sortable.js拖拽排序使用方法解析
2016/11/04 Javascript
jQuery实现导航高亮的方法【附demo源码下载】
2016/11/09 Javascript
详解bootstrap的modal-remote两种加载方式【强化】
2017/01/27 Javascript
javascript简单链式调用案例分析
2017/05/10 Javascript
Ionic + Angular.js实现图片轮播的方法示例
2017/05/21 Javascript
原生js 封装get ,post, delete 请求的实例
2017/08/11 Javascript
BootStrap模态框和select2合用时input无法获取焦点的解决方法
2017/09/01 Javascript
javascript计算渐变颜色的实例
2017/09/22 Javascript
vue中的event bus非父子组件通信解析
2017/10/27 Javascript
js中DOM事件绑定分析
2018/03/18 Javascript
vue升级之路之vue-router的使用教程
2018/08/14 Javascript
详解mpvue中使用vant时需要注意的onChange事件的坑
2019/05/16 Javascript
electron实现静默打印的示例代码
2019/08/12 Javascript
vue仿ios列表左划删除
2019/09/26 Javascript
使用kbone解决Vue项目同时支持小程序问题
2019/11/08 Javascript
Python中生成Epoch的方法
2017/04/26 Python
Python使用min、max函数查找二维数据矩阵中最小、最大值的方法
2018/05/15 Python
基于python中theano库的线性回归
2018/08/31 Python
python中强大的format函数实例详解
2018/12/05 Python
python3中类的继承以及self和super的区别详解
2019/06/26 Python
如何基于python把文字图片写入word文档
2020/07/31 Python
python如何实时获取tcpdump输出
2020/09/16 Python
python绘制雷达图实例讲解
2021/01/03 Python
CSS3中的Media Queries学习笔记
2016/05/23 HTML / CSS
英国太阳镜品牌:Taylor Morris Eyewear
2018/04/18 全球购物
解释一下钝化(Swap out)
2016/12/26 面试题
劲霸男装广告词
2014/03/21 职场文书
2014年大学生党员评议表自我评价
2014/09/20 职场文书
2014年团支部工作总结
2014/11/17 职场文书
Pytorch中的学习率衰减及其用法详解
2021/06/05 Python
Nginx本地配置SSL访问的实例教程
2022/05/30 Servers