fix(service): validate request body on /translate and /v1/translate

The two handlers ignored the error from c.BindJSON, so a malformed JSON body was silently processed as an empty payload. Check the error and return 400, consistent with /v2/translate.
This commit is contained in:
Bonny07 2026-05-31 23:54:26 +09:00
parent 3f099d187c
commit 09231c2650

View File

@ -108,7 +108,13 @@ func Router(cfg *Config) *gin.Engine {
// Free API endpoint, No Pro Account required
r.POST("/translate", authMiddleware(cfg), func(c *gin.Context) {
req := PayloadFree{}
c.BindJSON(&req)
if err := c.BindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": "Invalid request payload",
})
return
}
sourceLang := req.SourceLang
targetLang := req.TargetLang
@ -156,7 +162,13 @@ func Router(cfg *Config) *gin.Engine {
// Pro API endpoint, Pro Account required
r.POST("/v1/translate", authMiddleware(cfg), func(c *gin.Context) {
req := PayloadFree{}
c.BindJSON(&req)
if err := c.BindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": "Invalid request payload",
})
return
}
sourceLang := req.SourceLang
targetLang := req.TargetLang