Skip to content

Commit

Permalink
log request body without consuming it
Browse files Browse the repository at this point in the history
  • Loading branch information
JssDWt committed Nov 14, 2023
1 parent a9d8759 commit 3b519b9
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions http/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"bytes"
"errors"
"io"
"log"
Expand Down Expand Up @@ -85,6 +86,9 @@ func setupRouter(notifier *notify.Notifier) *gin.Engine {
func addWebHookRouter(r *gin.RouterGroup, notifier *notify.Notifier) {
r.POST("/notify", func(c *gin.Context) {

body, _ := io.ReadAll(c.Request.Body)
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))

// Make sure the query string fits the mobile push structure
var query MobilePushWebHookQuery
if err := c.ShouldBindQuery(&query); err != nil {
Expand All @@ -104,8 +108,7 @@ func addWebHookRouter(r *gin.RouterGroup, notifier *notify.Notifier) {
}

if validPayload == nil {
body, _ := io.ReadAll(c.Request.Body)
log.Printf("invalid payload, body: %v", string(body))
log.Printf("invalid payload, body: %s", body)
c.AbortWithError(http.StatusBadRequest, errors.New("unsupported payload"))
}

Expand Down

0 comments on commit 3b519b9

Please sign in to comment.