Skip to content

Commit

Permalink
[change] optimize request payload parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Oct 5, 2023
1 parent bf8dff3 commit f6a1267
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 f6a1267

Please sign in to comment.