diff --git a/api/api.go b/api/api.go index 74335d4..2147651 100644 --- a/api/api.go +++ b/api/api.go @@ -189,7 +189,7 @@ func (api *API) registerWebAppRoutes(router *gin.Engine) { } func (api *API) registerKOAPIRoutes(apiGroup *gin.RouterGroup) { - koGroup := apiGroup.Group("/ko") + koGroup := apiGroup.Group("/ko", koJSONMiddleware) // KO sync routes (webapp uses - progress & activity) koGroup.GET("/documents/:document/file", api.authKOMiddleware, api.createDownloadDocumentHandler(apiErrorPage)) @@ -325,6 +325,13 @@ func (api *API) loadTemplates( return nil } +func (api *API) templateMiddleware(router *gin.Engine) gin.HandlerFunc { + return func(c *gin.Context) { + router.HTMLRender = *api.generateTemplates() + c.Next() + } +} + func loggingMiddleware(c *gin.Context) { // Start timer startTime := time.Now() @@ -361,9 +368,12 @@ func loggingMiddleware(c *gin.Context) { log.WithFields(logData).Info(fmt.Sprintf("%s %s", c.Request.Method, c.Request.URL.Path)) } -func (api *API) templateMiddleware(router *gin.Engine) gin.HandlerFunc { - return func(c *gin.Context) { - router.HTMLRender = *api.generateTemplates() - c.Next() +// koJSONMiddleware forces JSON Content-Type to only return `application/json`. This is addressing +// the following issue: https://github.com/koreader/koreader/issues/13629 +func koJSONMiddleware(c *gin.Context) { + c.Next() + contentType := c.Writer.Header().Get("Content-Type") + if strings.HasPrefix(contentType, "application/json") { + c.Header("Content-Type", "application/json") } }