[英]golang gin gonic content-type not setting to application/json with c.JSON
根据官方文档, gin-gonic 的 c.JSON应该将响应头设置为application/json
,但是当我从Postman调用我的 API 时,响应头设置为text/plain; charset=utf-8
text/plain; charset=utf-8
我不明白我错过了什么,有什么想法吗?
文件:
函数 JSON
JSON 将给定的结构作为 JSON 序列化到响应正文中。 它还将 Content-Type 设置为“application/json”。
这是我的代码示例:
func postLogin(c *gin.Context) {
var credentials DTO.Credentials
if err := c.BindJSON(&credentials); err == nil {
c.JSON(buildResponse(services.CheckUserCredentials(credentials)))
} else {
var apiErrors = DTO.ApiErrors{}
for _, v := range err.(validator.ValidationErrors) {
apiErrors.Errors = append(apiErrors.Errors, DTO.ApiError{Field: v.Field, Message: v.Field + " is " + v.Tag})
}
c.JSON(http.StatusBadRequest, apiErrors)
}
}
编辑
经过调查, log.Println(c.Writer.Header().Get("Content-Type")) 没有打印任何东西,显示 content-type 应该是空的。
func writeContentType(w http.ResponseWriter, value []string) {
header := w.Header()
log.Println(header.Get("Content-Type")) // <=========== Nothing happen
if val := header["Content-Type"]; len(val) == 0 {
header["Content-Type"] = value
}
}
我真的不想将c.Writer.Header().Set("Content-Type", "application/json")
添加到我架构中的每条路由中......
编辑 2
似乎binding:"required"
打破了 Content-Type Header
type Credentials struct {
Email string `json:"email" binding:"required"`
Password string `json:"password" binding:"required"`
}
如果您希望所有请求都是 JSON,请添加一个中间件。
func JSONMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Content-Type", "application/json")
c.Next()
}
}
在您的路由器上添加
router.Use(JSONMiddleware())
看了下源码,好像已经设置了Content-Type
头就不会写了。
c.JSON
调用此函数,该函数调用以下代码:
func writeContentType(w http.ResponseWriter, value []string) {
header := w.Header()
if val := header["Content-Type"]; len(val) == 0 {
header["Content-Type"] = value
}
}
因此,您的Content-Type
必须设置在其他地方。
使用c.ShouldBindJSON(&credentials)
而不是c.BindJSON
。
这些方法在后台使用 MustBindWith。 如果存在绑定错误,则使用 c.AbortWithError(400, err).SetType(ErrorTypeBind) 中止请求。 这会将响应状态代码设置为 400,并将 Content-Type 标头设置为 text/plain; 字符集=utf-8。
如果您的输出实际上不是有效的 JSON 并且封送失败,也会发生这种情况。 当我没有在错误处理程序中返回时,我看到内容类型被设置为应用程序/文本,并且由于我的错误处理不当,我不小心将一个字符串连接到 JSON 上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.