Skip to content

Commit

Permalink
Add SetMultipartFormData func to Request (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Arnott authored May 11, 2020
1 parent 314627a commit 98f59aa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ func (r *Request) SetFileReader(param, fileName string, reader io.Reader) *Reque
return r
}

// SetMultipartFormData method allows simple form data to be attached to the request as `multipart:form-data`
func (r *Request) SetMultipartFormData(data map[string]string) *Request {

for k, v := range data {
r = r.SetMultipartField(k, "", "", strings.NewReader(v))
}

return r
}

// SetMultipartField method is to set custom data using io.Reader for multipart upload.
func (r *Request) SetMultipartField(param, fileName, contentType string, reader io.Reader) *Request {
r.isMultiPart = true
Expand Down
13 changes: 13 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,19 @@ func TestMultiPartUploadFileNotOnGetOrDelete(t *testing.T) {
assertEqual(t, "multipart content is not allowed in HTTP verb [DELETE]", err.Error())
}

func TestMultiPartFormData(t *testing.T) {
ts := createFormPostServer(t)
defer ts.Close()
resp, err := dclr().
SetMultipartFormData(map[string]string{"first_name": "Jeevanandam", "last_name": "M", "zip_code": "00001"}).
SetBasicAuth("myuser", "mypass").
Post(ts.URL + "/profile")

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

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

0 comments on commit 98f59aa

Please sign in to comment.