Skip to content

Commit 2744fe2

Browse files
fix(net/gclient): fix content-type 'application/json;charset=utf-8' … (gogf#4369)
fix(net/gclient): fix content-type 'application/json;charset=utf-8' can not match `application/json` --------- Co-authored-by: houseme <housemecn@gmail.com>
1 parent 1682cc9 commit 2744fe2

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

net/gclient/gclient_request.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"bytes"
1111
"context"
1212
"io"
13+
"mime"
1314
"mime/multipart"
1415
"net/http"
1516
"os"
@@ -172,7 +173,12 @@ func (c *Client) prepareRequest(ctx context.Context, method, url string, data ..
172173
allowFileUploading = true
173174
)
174175
if len(data) > 0 {
175-
switch c.header[httpHeaderContentType] {
176+
mediaType, _, err := mime.ParseMediaType(c.header[httpHeaderContentType])
177+
if err != nil {
178+
// Fallback: use the raw header value if parsing fails.
179+
mediaType = c.header[httpHeaderContentType]
180+
}
181+
switch mediaType {
176182
case httpHeaderContentTypeJson:
177183
switch data[0].(type) {
178184
case string, []byte:
@@ -206,7 +212,12 @@ func (c *Client) prepareRequest(ctx context.Context, method, url string, data ..
206212
if method == http.MethodGet {
207213
var bodyBuffer *bytes.Buffer
208214
if params != "" {
209-
switch c.header[httpHeaderContentType] {
215+
mediaType, _, err := mime.ParseMediaType(c.header[httpHeaderContentType])
216+
if err != nil {
217+
// Fallback: use the raw header value if parsing fails.
218+
mediaType = c.header[httpHeaderContentType]
219+
}
220+
switch mediaType {
210221
case
211222
httpHeaderContentTypeJson,
212223
httpHeaderContentTypeXml:

0 commit comments

Comments
 (0)