Skip to content

Commit

Permalink
feat(enhancement): multipart populate filename from path if file path…
Browse files Browse the repository at this point in the history
… value is empty (#944)
  • Loading branch information
jeevatkm authored Jan 7, 2025
1 parent c6de761 commit ea07167
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ func (c *Client) FormData() url.Values {
}

// SetFormData method sets Form parameters and their values in the client instance.
// It applies only to HTTP methods `POST` and `PUT`, and the request content type would be set as
// `application/x-www-form-urlencoded`. These form data will be added to all the requests raised from
// this client instance. Also, it can be overridden at the request level.
// The request content type would be set as `application/x-www-form-urlencoded`.
// The client-level form data gets added to all the requests. Also, it can be
// overridden at the request level.
//
// See [Request.SetFormData].
//
Expand Down
5 changes: 5 additions & 0 deletions multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"net/textproto"
"os"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -104,6 +105,10 @@ func (mf *MultipartField) openFileIfRequired() error {
return err
}

if isStringEmpty(mf.FileName) {
mf.FileName = filepath.Base(mf.FilePath)
}

// if file open is success, stat will succeed
fileStat, _ := file.Stat()

Expand Down
6 changes: 6 additions & 0 deletions multipart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ func TestMultipartFieldProgressCallback(t *testing.T) {
}

fields := []*MultipartField{
{
Name: "test-image",
FilePath: filepath.Join(getTestDataPath(), "test-img.png"),
ProgressCallback: progressCallback,
},
{
Name: "test-image-1",
FileName: "test-image-1.png",
Expand Down Expand Up @@ -456,6 +461,7 @@ func TestMultipartFieldProgressCallback(t *testing.T) {
assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, true, strings.Contains(responseStr, "test-image-1.png"))
assertEqual(t, true, strings.Contains(responseStr, "test-img.png"))
assertEqual(t, true, strings.Contains(responseStr, "50mbfile.bin"))
assertEqual(t, true, strings.Contains(responseStr, "100mbfile.bin"))
}
Expand Down
5 changes: 2 additions & 3 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,8 @@ func (r *Request) SetQueryString(query string) *Request {
return r
}

// SetFormData method sets Form parameters and their values for the current request.
// It applies only to HTTP methods `POST` and `PUT`, and by default requests
// content type would be set as `application/x-www-form-urlencoded`.
// SetFormData method sets form parameters and their values in the current request.
// The request content type would be set as `application/x-www-form-urlencoded`.
//
// client.R().
// SetFormData(map[string]string{
Expand Down

0 comments on commit ea07167

Please sign in to comment.