Skip to content

Commit

Permalink
Merge pull request #213 from yflau/main
Browse files Browse the repository at this point in the history
avoid error when json body not object
  • Loading branch information
trheyi authored Jul 27, 2024
2 parents 42883e5 + 520a00b commit d2d4122
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"strings"
"unicode"

"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
Expand Down Expand Up @@ -358,6 +359,11 @@ func (path Path) setPayload(c *gin.Context) {

}

if !isFirstNonSpaceChar(string(bytes), '{') {
c.Set("__payloads", map[string]interface{}{})
return
}

payloads := map[string]interface{}{}
err = jsoniter.Unmarshal(bytes, &payloads)
if err != nil {
Expand All @@ -369,3 +375,12 @@ func (path Path) setPayload(c *gin.Context) {

}
}

func isFirstNonSpaceChar(text string, char rune) bool {
for _, r := range text {
if !unicode.IsSpace(r) {
return r == char
}
}
return false
}

0 comments on commit d2d4122

Please sign in to comment.