Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to set custom multipart boundary value #820

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 8 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,13 @@ func (r *Request) SetMultipartFields(fields ...*MultipartField) *Request {
return r
}

// SetBoundary method sets the multipart boundary for the request
// By default a random boundary will be generated in `mime/multipart`.
func (r *Request) SetBoundary(boundary string) *Request {
Copy link
Member

@jeevatkm jeevatkm Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PokeGuys Thanks for adding the use case detail to the PR description.

Can you refactor this method as below and it includes updated godoc and version info too?

// 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
 }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

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"}).
SetBoundary(`"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"}).
SetBoundary("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