Skip to content

Commit

Permalink
request默认query,排除未导出字段
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteReimu committed Apr 2, 2024
1 parent 9dd872d commit c73b486
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func withParams(r *resty.Request, in any) error {
contentType := ""
for i := 0; i < inType.NumField(); i++ {
fieldType := inType.Field(i)
if !fieldType.IsExported() {
continue
}
fieldValue := inValue.Field(i)
tValue := fieldType.Tag.Get("request")

Expand Down Expand Up @@ -150,19 +153,22 @@ func withParams(r *resty.Request, in any) error {
}
}

contentType = "application/x-www-form-urlencoded"
for name := range tagMap {
switch name {
case "query":
contentType = "application/x-www-form-urlencoded"
r.SetQueryParam(fieldName, cast.ToString(realVal))
case "json":
contentType = "application/json"
bodyMap[fieldName] = realVal
case "form-data":
contentType = "multipart/form-data"
bodyMap[fieldName] = realVal
}
}
if contentType == "application/x-www-form-urlencoded" {
r.SetQueryParam(fieldName, cast.ToString(realVal))
} else {
bodyMap[fieldName] = realVal
}
}

r.SetHeader("Content-Type", contentType)
Expand Down

0 comments on commit c73b486

Please sign in to comment.