Skip to content

Commit

Permalink
fix: content type (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi authored Mar 2, 2024
1 parent 5eb04b0 commit 5a7d285
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ import (
type ContextRequest struct {
ctx *Context
instance *fiber.Ctx
postData map[string]any
httpBody map[string]any
log log.Log
validation contractsvalidate.Validation
}

func NewContextRequest(ctx *Context, log log.Log, validation contractsvalidate.Validation) contractshttp.ContextRequest {
postData, err := getPostData(ctx)
httpBody, err := getHttpBody(ctx)
if err != nil {
LogFacade.Error(fmt.Sprintf("%+v", errors.Unwrap(err)))
}

return &ContextRequest{ctx: ctx, instance: ctx.instance, postData: postData, log: log, validation: validation}
return &ContextRequest{ctx: ctx, instance: ctx.instance, httpBody: httpBody, log: log, validation: validation}
}

func (r *ContextRequest) AbortWithStatus(code int) {
Expand All @@ -60,7 +60,7 @@ func (r *ContextRequest) All() map[string]any {
for k, v := range r.instance.Queries() {
data[k] = v
}
for k, v := range r.postData {
for k, v := range r.httpBody {
data[k] = v
}

Expand Down Expand Up @@ -255,7 +255,7 @@ func (r *ContextRequest) Path() string {

func (r *ContextRequest) Input(key string, defaultValue ...string) string {
keys := strings.Split(key, ".")
current := r.postData
current := r.httpBody
for _, k := range keys {
value, found := current[k]
if found {
Expand All @@ -281,7 +281,7 @@ func (r *ContextRequest) Input(key string, defaultValue ...string) string {

func (r *ContextRequest) InputArray(key string, defaultValue ...[]string) []string {
keys := strings.Split(key, ".")
current := r.postData
current := r.httpBody
for _, k := range keys {
value, found := current[k]
if !found {
Expand All @@ -303,7 +303,7 @@ func (r *ContextRequest) InputArray(key string, defaultValue ...[]string) []stri

func (r *ContextRequest) InputMap(key string, defaultValue ...map[string]string) map[string]string {
keys := strings.Split(key, ".")
current := r.postData
current := r.httpBody
for _, k := range keys {
value, found := current[k]
if !found {
Expand Down Expand Up @@ -433,23 +433,23 @@ func (r *ContextRequest) ValidateRequest(request contractshttp.FormRequest) (con
return validator.Errors(), nil
}

func getPostData(ctx *Context) (map[string]any, error) {
func getHttpBody(ctx *Context) (map[string]any, error) {
if len(ctx.instance.Request().Body()) == 0 {
return nil, nil
}

contentType := ctx.instance.Get("Content-Type")
data := make(map[string]any)

if contentType == "application/json" {
if strings.Contains(contentType, "application/json") {
bodyBytes := ctx.instance.Body()

if err := json.Unmarshal(bodyBytes, &data); err != nil {
return nil, fmt.Errorf("decode json [%v] error: %v", utils.UnsafeString(bodyBytes), err)
}
}

if contentType == "multipart/form-data" {
if strings.Contains(contentType, "multipart/form-data") {
if form, err := ctx.instance.MultipartForm(); err == nil {
for k, v := range form.Value {
data[k] = strings.Join(v, ",")
Expand All @@ -462,7 +462,7 @@ func getPostData(ctx *Context) (map[string]any, error) {
}
}

if contentType == "application/x-www-form-urlencoded" {
if strings.Contains(contentType, "application/x-www-form-urlencoded") {
args := ctx.instance.Request().PostArgs()
args.VisitAll(func(key, value []byte) {
data[utils.UnsafeString(key)] = utils.UnsafeString(value)
Expand Down

0 comments on commit 5a7d285

Please sign in to comment.