Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c2a61934
authored
Dec 20, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(gin): improve request body handling and error reporting
parent
3523acfc
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
5 deletions
+13
-5
common/gin.go
+13
-5
No files found.
common/gin.go
View file @
c2a61934
...
...
@@ -2,7 +2,7 @@ package common
import
(
"bytes"
"
errors
"
"
fmt
"
"io"
"mime"
"mime/multipart"
...
...
@@ -12,6 +12,7 @@ import (
"time"
"github.com/QuantumNous/new-api/constant"
"github.com/pkg/errors"
"github.com/gin-gonic/gin"
)
...
...
@@ -39,8 +40,15 @@ func GetRequestBody(c *gin.Context) ([]byte, error) {
}
}
maxMB
:=
constant
.
MaxRequestBodyMB
if
maxMB
<=
0
{
maxMB
=
32
if
maxMB
<
0
{
// no limit
body
,
err
:=
io
.
ReadAll
(
c
.
Request
.
Body
)
_
=
c
.
Request
.
Body
.
Close
()
if
err
!=
nil
{
return
nil
,
err
}
c
.
Set
(
KeyRequestBody
,
body
)
return
body
,
nil
}
maxBytes
:=
int64
(
maxMB
)
<<
20
...
...
@@ -49,13 +57,13 @@ func GetRequestBody(c *gin.Context) ([]byte, error) {
if
err
!=
nil
{
_
=
c
.
Request
.
Body
.
Close
()
if
IsRequestBodyTooLargeError
(
err
)
{
return
nil
,
ErrRequestBodyTooLarge
return
nil
,
errors
.
Wrap
(
ErrRequestBodyTooLarge
,
fmt
.
Sprintf
(
"request body exceeds %d MB"
,
maxMB
))
}
return
nil
,
err
}
_
=
c
.
Request
.
Body
.
Close
()
if
int64
(
len
(
body
))
>
maxBytes
{
return
nil
,
ErrRequestBodyTooLarge
return
nil
,
errors
.
Wrap
(
ErrRequestBodyTooLarge
,
fmt
.
Sprintf
(
"request body exceeds %d MB"
,
maxMB
))
}
c
.
Set
(
KeyRequestBody
,
body
)
return
body
,
nil
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment