Skip to content

Commit 0c404fd

Browse files
Fix JSON Content-Type: use 'application/json' without charset=utf-8
Gin's c.JSON() appends '; charset=utf-8' to Content-Type which fails the validator's exact match check. Switched to manual json.Marshal + c.Data() with explicit Content-Type header.
1 parent 9338e54 commit 0c404fd

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

frameworks/gin/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ func main() {
187187
}
188188
}
189189
c.Header("Server", "gin")
190-
c.JSON(http.StatusOK, ProcessResponse{Items: items, Count: len(items)})
190+
c.Header("Content-Type", "application/json")
191+
data, _ := json.Marshal(ProcessResponse{Items: items, Count: len(items)})
192+
c.Data(http.StatusOK, "application/json", data)
191193
})
192194

193195
r.GET("/compression", func(c *gin.Context) {
@@ -263,7 +265,9 @@ func main() {
263265
})
264266
}
265267
c.Header("Server", "gin")
266-
c.JSON(http.StatusOK, gin.H{"items": items, "count": len(items)})
268+
c.Header("Content-Type", "application/json")
269+
data, _ := json.Marshal(gin.H{"items": items, "count": len(items)})
270+
c.Data(http.StatusOK, "application/json", data)
267271
})
268272

269273
r.GET("/static/:filename", func(c *gin.Context) {

0 commit comments

Comments
 (0)