目录

go http body err: invalid character ï looking for beginning of value

go http 请求解析body err: invalid character ‘ï’ looking for beginning of value

问题

在使用 go http 发起请求后,对响应 body 进行 json 解析时报错:

invalid character 'ï' looking for beginning of value

解决

原因:

The server is sending you a UTF-8 text 
string with a Byte Order Mark (BOM). 
The BOM identifies that the text is UTF-8 encoded,
but it should be removed before decoding.

大概意思就是说:字符串字节中存在特殊字符(BOM),导致 json 反序列化失败

解决

在 unmarshal 反序列化之前将服务器返回的body处理一下:

 // Or []byte{239, 187, 191}
body = bytes.TrimPrefix(body, []byte("\xef\xbb\xbf"))

参考资料

go json 序列化 反序列化 报错 invalid character i looking for beginning of value - 五岁小孩的博客

版权声明

未经授权,禁止转载本文章。
如需转载请保留原文链接并注明出处。即视为默认获得授权。
未保留原文链接未注明出处或删除链接将视为侵权,必追究法律责任!

本文原文链接: https://fiveyoboy.com/articles/go-http-err-1/

备用原文链接: https://blog.fiveyoboy.com/articles/go-http-err-1/