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

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:
Bonny Lin 2026-06-02 15:32:22 +09:00 committed by GitHub
parent 3f099d187c
commit d19a3902df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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