Skip to content

Commit

Permalink
feat(enhancement): add SetContentType helper method (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm authored Jan 14, 2025
1 parent 1ce39e1 commit d55d1f6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
14 changes: 7 additions & 7 deletions multipart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ func TestMultipartLargeFile(t *testing.T) {
c := dcnl()
resp, err := c.R().
SetFile("file", filepath.Join(getTestDataPath(), "test-img.png")).
SetMultipartBoundary("custom-boundary-"+strconv.FormatInt(time.Now().Unix(), 10)).
SetHeader("Content-Type", "image/png").
SetMultipartBoundary("custom-boundary-" + strconv.FormatInt(time.Now().Unix(), 10)).
SetContentType("image/png").
Post(ts.URL + "/upload")
assertNil(t, err)
assertNotNil(t, resp)
Expand All @@ -365,8 +365,8 @@ func TestMultipartLargeFile(t *testing.T) {
c := dcnl()
_, err := c.R().
SetFile("file", filepath.Join(getTestDataPath(), "test-img.png")).
SetMultipartBoundary(`"custom-boundary-"`+strconv.FormatInt(time.Now().Unix(), 10)).
SetHeader("Content-Type", "image/png").
SetMultipartBoundary(`"custom-boundary-"` + strconv.FormatInt(time.Now().Unix(), 10)).
SetContentType("image/png").
Post(ts.URL + "/upload")
assertNotNil(t, err)
assertEqual(t, "mime: invalid boundary character", err.Error())
Expand Down Expand Up @@ -616,8 +616,8 @@ func TestMultipartRequest_createMultipart(t *testing.T) {

req1 := c.R().
SetFile("file", filepath.Join(getTestDataPath(), "test-img.png")).
SetMultipartBoundary("custom-boundary-"+strconv.FormatInt(time.Now().Unix(), 10)).
SetHeader("Content-Type", "image/png")
SetMultipartBoundary("custom-boundary-" + strconv.FormatInt(time.Now().Unix(), 10)).
SetContentType("image/png")

mw := multipart.NewWriter(new(bytes.Buffer))
err := createMultipart(mw, req1)
Expand All @@ -638,7 +638,7 @@ func TestMultipartRequest_createMultipart(t *testing.T) {

req1 := c.R().
SetFile("file", filepath.Join(getTestDataPath(), "test-img.png")).
SetHeader("Content-Type", "image/png")
SetContentType("image/png")

mw := multipart.NewWriter(new(bytes.Buffer))
err := createMultipart(mw, req1)
Expand Down
8 changes: 8 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ func (r *Request) WithContext(ctx context.Context) *Request {
return rr
}

// SetContentType method is a convenient way to set the header Content-Type in the request
//
// client.R().SetContentType("application/json")
func (r *Request) SetContentType(ct string) *Request {
r.SetHeader(hdrContentTypeKey, ct)
return r
}

// SetHeader method sets a single header field and its value in the current request.
//
// For Example: To set `Content-Type` and `Accept` as `application/json`.
Expand Down
4 changes: 2 additions & 2 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ func TestRequestFileUploadAsReader(t *testing.T) {

resp, err := dcnldr().
SetBody(file).
SetHeader("Content-Type", "image/png").
SetContentType("image/png").
Post(ts.URL + "/upload")

assertError(t, err)
Expand All @@ -1606,7 +1606,7 @@ func TestRequestFileUploadAsReader(t *testing.T) {

resp, err = dcnldr().
SetBody(file).
SetHeader("Content-Type", "image/png").
SetContentType("image/png").
SetContentLength(true).
Post(ts.URL + "/upload")

Expand Down

0 comments on commit d55d1f6

Please sign in to comment.