Skip to content

Commit

Permalink
feat: add ability to set custom multipart boundary value (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
PokeGuys authored Aug 15, 2024
1 parent 231e19c commit f575bf6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,13 @@ func handleMultipart(c *Client, r *Request) error {
r.bodyBuf = acquireBuffer()
w := multipart.NewWriter(r.bodyBuf)

// Set boundary if not set by user
if r.multipartBoundary != "" {
if err := w.SetBoundary(r.multipartBoundary); err != nil {
return err
}
}

for k, v := range c.FormData {
for _, iv := range v {
if err := w.WriteField(k, iv); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Request struct {
bodyBuf *bytes.Buffer
clientTrace *clientTrace
log Logger
multipartBoundary string
multipartFiles []*File
multipartFields []*MultipartField
retryConditions []RetryConditionFunc
Expand Down Expand Up @@ -458,6 +459,15 @@ func (r *Request) SetMultipartFields(fields ...*MultipartField) *Request {
return r
}

// SetMultipartBoundary method sets the custom multipart boundary for the multipart request.
// Typically, the `mime/multipart` package generates a random multipart boundary, if not provided.
//
// Since v2.15.0
func (r *Request) SetMultipartBoundary(boundary string) *Request {
r.multipartBoundary = boundary
return r
}

// SetContentLength method sets the HTTP header `Content-Length` value for current request.
// By default Resty won't set `Content-Length`. Also you have an option to enable for every
// request.
Expand Down
23 changes: 23 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,29 @@ func TestMultiPartMultipartFields(t *testing.T) {
assertEqual(t, true, strings.Contains(responseStr, "upload-file-2.json"))
}

func TestMultiPartCustomBoundary(t *testing.T) {
ts := createFormPostServer(t)
defer ts.Close()
defer cleanupFiles(".testdata/upload")

_, err := dclr().
SetMultipartFormData(map[string]string{"first_name": "Jeevanandam", "last_name": "M", "zip_code": "00001"}).
SetMultipartBoundary(`"my-custom-boundary"`).
SetBasicAuth("myuser", "mypass").
Post(ts.URL + "/profile")

assertEqual(t, "mime: invalid boundary character", err.Error())

resp, err := dclr().
SetMultipartFormData(map[string]string{"first_name": "Jeevanandam", "last_name": "M", "zip_code": "00001"}).
SetMultipartBoundary("my-custom-boundary").
Post(ts.URL + "/profile")

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, "Success", resp.String())
}

func TestGetWithCookie(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()
Expand Down

0 comments on commit f575bf6

Please sign in to comment.