diff --git a/context_request.go b/context_request.go index 109f434..7ec98fa 100644 --- a/context_request.go +++ b/context_request.go @@ -433,7 +433,7 @@ func getPostData(ctx *Context) (map[string]any, error) { 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 { @@ -441,7 +441,7 @@ func getPostData(ctx *Context) (map[string]any, error) { } } - 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, ",") @@ -454,7 +454,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[string(key)] = string(value) diff --git a/context_request_test.go b/context_request_test.go index 1977754..f17a241 100644 --- a/context_request_test.go +++ b/context_request_test.go @@ -927,6 +927,7 @@ func TestRequest(t *testing.T) { mockStorage.On("Disk", "local").Return(mockDriver).Once() frameworkfilesystem.StorageFacade = mockStorage + name := ctx.Request().Input("name") fileInfo, err := ctx.Request().File("file") mockDriver.On("PutFile", "test", fileInfo).Return("test/README.md", nil).Once() @@ -947,10 +948,11 @@ func TestRequest(t *testing.T) { return ctx.Response().Success().Json(contractshttp.Json{ "exist": mockStorage.Exists(filePath), + "extension": extension, + "file_path_length": len(filePath), "hash_name_length": len(fileInfo.HashName()), "hash_name_length1": len(fileInfo.HashName("test")), - "file_path_length": len(filePath), - "extension": extension, + "name": name, "original_name": fileInfo.GetClientOriginalName(), "original_extension": fileInfo.GetClientOriginalExtension(), }) @@ -962,7 +964,13 @@ func TestRequest(t *testing.T) { if err != nil { return err } + defer readme.Close() + + if err := writer.WriteField("name", "goravel"); err != nil { + return err + } + part1, err := writer.CreateFormFile("file", filepath.Base("./README.md")) if err != nil { return err @@ -976,13 +984,17 @@ func TestRequest(t *testing.T) { return err } - req, _ = http.NewRequest(method, url, payload) + req, err = http.NewRequest(method, url, payload) + if err != nil { + return err + } + req.Header.Set("Content-Type", writer.FormDataContentType()) return nil }, expectCode: http.StatusOK, - expectBodyJson: "{\"exist\":true,\"extension\":\"txt\",\"file_path_length\":14,\"hash_name_length\":44,\"hash_name_length1\":49,\"original_extension\":\"md\",\"original_name\":\"README.md\"}", + expectBodyJson: "{\"exist\":true,\"extension\":\"txt\",\"file_path_length\":14,\"hash_name_length\":44,\"hash_name_length1\":49,\"name\":\"goravel\",\"original_extension\":\"md\",\"original_name\":\"README.md\"}", }, { name: "GET with params and validator, validate pass",