Skip to content

Commit

Permalink
Merge pull request #156 from trheyi/main
Browse files Browse the repository at this point in the history
[change] optimize request payload parsing
  • Loading branch information
trheyi authored Oct 5, 2023
2 parents e020726 + f6a1267 commit 83e16bc
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"strings"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -272,21 +271,33 @@ func (path Path) setResponseHeaders(c *gin.Context, resp interface{}, contentTyp
func (path Path) setPayload(c *gin.Context) {

if strings.HasPrefix(strings.ToLower(c.GetHeader("content-type")), "application/json") {
bytes, err := ioutil.ReadAll(c.Request.Body)

if c.Request.Body == nil {
c.Set("__payloads", map[string]interface{}{})
return
}

bytes, err := io.ReadAll(c.Request.Body)
if err != nil {
panic(err)
c.Set("__payloads", map[string]interface{}{})
log.Error("[Path] %s %s", path.Path, err.Error())
return
}

if bytes == nil || len(bytes) == 0 {
c.Set("__payloads", map[string]interface{}{})
return

} else {
payloads := map[string]interface{}{}
err = jsoniter.Unmarshal(bytes, &payloads)
if err != nil {
panic(err)
}
c.Set("__payloads", payloads)
}

payloads := map[string]interface{}{}
err = jsoniter.Unmarshal(bytes, &payloads)
if err != nil {
c.Set("__payloads", map[string]interface{}{})
log.Error("[Path] %s %s", path.Path, err.Error())
}
c.Set("__payloads", payloads)
c.Request.Body = io.NopCloser(strings.NewReader(string(bytes)))

}
}

0 comments on commit 83e16bc

Please sign in to comment.