Skip to content

Commit

Permalink
feat: 自定义contentType和body (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: xiongchuanhong <34299652+xiongchuanhong@users.noreply.github.com>
  • Loading branch information
HobbyBear and xiongchuanhong authored Feb 6, 2020
1 parent f27c03e commit aebcaf2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion before_request_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Test_setRequestHeader(t *testing.T) {
}{
Age: 13,
}

data, _ := xml.Marshal(xmlBody)
tests := [...]struct {
request *Request
want string
Expand All @@ -81,6 +81,11 @@ func Test_setRequestHeader(t *testing.T) {
request: c.NewRequest().WithXMLBody(xmlBody).Post(),
want: "application/xml; charset=utf-8",
},
3: {
request: c.NewRequest().WithCustomBody("application/xml; charset=utf-8",
data).Post(),
want: "application/xml; charset=utf-8",
},
}

for i, tt := range tests {
Expand Down
7 changes: 7 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ func (r *Request) WithMultipartFormDataBody(formData ...*FormData) *Request {
}
return r
}
func (r *Request) WithCustomBody(contentType string, body []byte) *Request {
r.body = &requestCustomBody{
payload: body,
contentType: contentType,
}
return r
}

// WithTimeout sets the request timeout.
func (r *Request) WithTimeout(timeout time.Duration) *Request {
Expand Down
13 changes: 13 additions & 0 deletions request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ type requestBody interface {
Body() ([]byte, error)
}

type requestCustomBody struct {
payload []byte
contentType string
}

func (body *requestCustomBody) ContentType() string {
return body.contentType
}

func (body *requestCustomBody) Body() ([]byte, error) {
return body.payload, nil
}

type requestJSONBody struct {
payload interface{}
}
Expand Down

0 comments on commit aebcaf2

Please sign in to comment.