From 98f59aafc516d226eb7a688384882eda5b9cabe3 Mon Sep 17 00:00:00 2001 From: Steven Arnott Date: Sun, 10 May 2020 21:29:05 -0400 Subject: [PATCH] Add SetMultipartFormData func to Request (#337) --- request.go | 10 ++++++++++ request_test.go | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/request.go b/request.go index 776a26e5..7d209032 100644 --- a/request.go +++ b/request.go @@ -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 diff --git a/request_test.go b/request_test.go index 3bf1f169..22b97eda 100644 --- a/request_test.go +++ b/request_test.go @@ -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()