Skip to content

Commit

Permalink
fix(force_type): forceType not working while it's only be changed in …
Browse files Browse the repository at this point in the history
…getResponseBytes

fix parnurzeal#217 and
parnurzeal#204
  • Loading branch information
wklken committed Jan 25, 2022
1 parent 3c96ee1 commit f25a6c1
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions gorequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,25 +1147,6 @@ func (s *SuperAgent) getResponseBytes() (Response, []byte, []error) {
if len(s.Errors) != 0 {
return nil, nil, s.Errors
}
// check if there is forced type
switch s.ForceType {
case TypeJSON, TypeForm, TypeXML, TypeText, TypeMultipart:
s.TargetType = s.ForceType
// If forcetype is not set, check whether user set Content-Type header.
// If yes, also bounce to the correct supported TargetType automatically.
default:
contentType := s.Header.Get("Content-Type")
for k, v := range Types {
if contentType == v {
s.TargetType = k
}
}
}

// if slice and map get mixed, let's bounce to rawstring
if len(s.Data) != 0 && len(s.SliceData) != 0 {
s.BounceToRawString = true
}

// Make Request
req, err = s.MakeRequest()
Expand Down Expand Up @@ -1236,6 +1217,26 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) {
err error
)

// check if there is forced type
switch s.ForceType {
case TypeJSON, TypeForm, TypeXML, TypeText, TypeMultipart:
s.TargetType = s.ForceType
// If forcetype is not set, check whether user set Content-Type header.
// If yes, also bounce to the correct supported TargetType automatically.
default:
contentType := s.Header.Get("Content-Type")
for k, v := range Types {
if contentType == v {
s.TargetType = k
}
}
}

// if slice and map get mixed, let's bounce to rawstring
if len(s.Data) != 0 && len(s.SliceData) != 0 {
s.BounceToRawString = true
}

if s.Method == "" {
return nil, fmt.Errorf("no method specified")
}
Expand Down

0 comments on commit f25a6c1

Please sign in to comment.