Skip to content

Commit ea07167

Browse files
authored
feat(enhancement): multipart populate filename from path if file path value is empty (#944)
1 parent c6de761 commit ea07167

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,9 @@ func (c *Client) FormData() url.Values {
475475
}
476476

477477
// SetFormData method sets Form parameters and their values in the client instance.
478-
// It applies only to HTTP methods `POST` and `PUT`, and the request content type would be set as
479-
// `application/x-www-form-urlencoded`. These form data will be added to all the requests raised from
480-
// this client instance. Also, it can be overridden at the request level.
478+
// The request content type would be set as `application/x-www-form-urlencoded`.
479+
// The client-level form data gets added to all the requests. Also, it can be
480+
// overridden at the request level.
481481
//
482482
// See [Request.SetFormData].
483483
//

multipart.go

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"net/textproto"
1212
"os"
13+
"path/filepath"
1314
"strings"
1415
)
1516

@@ -104,6 +105,10 @@ func (mf *MultipartField) openFileIfRequired() error {
104105
return err
105106
}
106107

108+
if isStringEmpty(mf.FileName) {
109+
mf.FileName = filepath.Base(mf.FilePath)
110+
}
111+
107112
// if file open is success, stat will succeed
108113
fileStat, _ := file.Stat()
109114

multipart_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,11 @@ func TestMultipartFieldProgressCallback(t *testing.T) {
420420
}
421421

422422
fields := []*MultipartField{
423+
{
424+
Name: "test-image",
425+
FilePath: filepath.Join(getTestDataPath(), "test-img.png"),
426+
ProgressCallback: progressCallback,
427+
},
423428
{
424429
Name: "test-image-1",
425430
FileName: "test-image-1.png",
@@ -456,6 +461,7 @@ func TestMultipartFieldProgressCallback(t *testing.T) {
456461
assertError(t, err)
457462
assertEqual(t, http.StatusOK, resp.StatusCode())
458463
assertEqual(t, true, strings.Contains(responseStr, "test-image-1.png"))
464+
assertEqual(t, true, strings.Contains(responseStr, "test-img.png"))
459465
assertEqual(t, true, strings.Contains(responseStr, "50mbfile.bin"))
460466
assertEqual(t, true, strings.Contains(responseStr, "100mbfile.bin"))
461467
}

request.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,8 @@ func (r *Request) SetQueryString(query string) *Request {
297297
return r
298298
}
299299

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

0 commit comments

Comments
 (0)