Skip to content

Commit

Permalink
Allow bodies to all HTTP methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pkopac committed Feb 10, 2017
1 parent f441e24 commit ab5fdf7
Showing 1 changed file with 90 additions and 96 deletions.
186 changes: 90 additions & 96 deletions gorequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,112 +1091,106 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) {
err error
)

switch s.Method {
case POST, PUT, PATCH:
if s.TargetType == "json" {
// If-case to give support to json array. we check if
// 1) Map only: send it as json map from s.Data
// 2) Array or Mix of map & array or others: send it as rawstring from s.RawString
var contentJson []byte
if s.BounceToRawString {
contentJson = []byte(s.RawString)
} else if len(s.Data) != 0 {
contentJson, _ = json.Marshal(s.Data)
} else if len(s.SliceData) != 0 {
contentJson, _ = json.Marshal(s.SliceData)
}
contentReader := bytes.NewReader(contentJson)
req, err = http.NewRequest(s.Method, s.Url, contentReader)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
} else if s.TargetType == "form" || s.TargetType == "form-data" || s.TargetType == "urlencoded" {
var contentForm []byte
if s.BounceToRawString || len(s.SliceData) != 0 {
contentForm = []byte(s.RawString)
} else {
formData := changeMapToURLValues(s.Data)
contentForm = []byte(formData.Encode())
}
contentReader := bytes.NewReader(contentForm)
req, err = http.NewRequest(s.Method, s.Url, contentReader)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} else if s.TargetType == "text" {
req, err = http.NewRequest(s.Method, s.Url, strings.NewReader(s.RawString))
req.Header.Set("Content-Type", "text/plain")
} else if s.TargetType == "xml" {
req, err = http.NewRequest(s.Method, s.Url, strings.NewReader(s.RawString))
req.Header.Set("Content-Type", "application/xml")
} else if s.TargetType == "multipart" {

var buf bytes.Buffer
mw := multipart.NewWriter(&buf)

if s.BounceToRawString {
fieldName, ok := s.Header["data_fieldname"]
if !ok {
fieldName = "data"
}
fw, _ := mw.CreateFormField(fieldName)
fw.Write([]byte(s.RawString))
if s.Method == "" {
return nil, errors.New("No method specified")
}

if s.TargetType == "json" {
// If-case to give support to json array. we check if
// 1) Map only: send it as json map from s.Data
// 2) Array or Mix of map & array or others: send it as rawstring from s.RawString
var contentJson []byte
if s.BounceToRawString {
contentJson = []byte(s.RawString)
} else if len(s.Data) != 0 {
contentJson, _ = json.Marshal(s.Data)
} else if len(s.SliceData) != 0 {
contentJson, _ = json.Marshal(s.SliceData)
}
contentReader := bytes.NewReader(contentJson)
req, err = http.NewRequest(s.Method, s.Url, contentReader)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
} else if s.TargetType == "form" || s.TargetType == "form-data" || s.TargetType == "urlencoded" {
var contentForm []byte
if s.BounceToRawString || len(s.SliceData) != 0 {
contentForm = []byte(s.RawString)
} else {
formData := changeMapToURLValues(s.Data)
contentForm = []byte(formData.Encode())
}
contentReader := bytes.NewReader(contentForm)
req, err = http.NewRequest(s.Method, s.Url, contentReader)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} else if s.TargetType == "text" {
req, err = http.NewRequest(s.Method, s.Url, strings.NewReader(s.RawString))
req.Header.Set("Content-Type", "text/plain")
} else if s.TargetType == "xml" {
req, err = http.NewRequest(s.Method, s.Url, strings.NewReader(s.RawString))
req.Header.Set("Content-Type", "application/xml")
} else if s.TargetType == "multipart" {

var buf bytes.Buffer
mw := multipart.NewWriter(&buf)

if s.BounceToRawString {
fieldName, ok := s.Header["data_fieldname"]
if !ok {
fieldName = "data"
}
fw, _ := mw.CreateFormField(fieldName)
fw.Write([]byte(s.RawString))
}

if len(s.Data) != 0 {
formData := changeMapToURLValues(s.Data)
for key, values := range formData {
for _, value := range values {
fw, _ := mw.CreateFormField(key)
fw.Write([]byte(value))
}
if len(s.Data) != 0 {
formData := changeMapToURLValues(s.Data)
for key, values := range formData {
for _, value := range values {
fw, _ := mw.CreateFormField(key)
fw.Write([]byte(value))
}
}
}

if len(s.SliceData) != 0 {
fieldName, ok := s.Header["json_fieldname"]
if !ok {
fieldName = "data"
}
// copied from CreateFormField() in mime/multipart/writer.go
h := make(textproto.MIMEHeader)
fieldName = strings.Replace(strings.Replace(fieldName, "\\", "\\\\", -1), `"`, "\\\"", -1)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"`, fieldName))
h.Set("Content-Type", "application/json")
fw, _ := mw.CreatePart(h)
contentJson, err := json.Marshal(s.SliceData)
if err != nil {
return nil, err
}
fw.Write(contentJson)
if len(s.SliceData) != 0 {
fieldName, ok := s.Header["json_fieldname"]
if !ok {
fieldName = "data"
}
// copied from CreateFormField() in mime/multipart/writer.go
h := make(textproto.MIMEHeader)
fieldName = strings.Replace(strings.Replace(fieldName, "\\", "\\\\", -1), `"`, "\\\"", -1)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"`, fieldName))
h.Set("Content-Type", "application/json")
fw, _ := mw.CreatePart(h)
contentJson, err := json.Marshal(s.SliceData)
if err != nil {
return nil, err
}
fw.Write(contentJson)
}

// add the files
if len(s.FileData) != 0 {
for _, file := range s.FileData {
fw, _ := mw.CreateFormFile(file.Fieldname, file.Filename)
fw.Write(file.Data)
}
// add the files
if len(s.FileData) != 0 {
for _, file := range s.FileData {
fw, _ := mw.CreateFormFile(file.Fieldname, file.Filename)
fw.Write(file.Data)
}
}

// close before call to FormDataContentType ! otherwise its not valid multipart
mw.Close()
// close before call to FormDataContentType ! otherwise its not valid multipart
mw.Close()

req, err = http.NewRequest(s.Method, s.Url, &buf)
req.Header.Set("Content-Type", mw.FormDataContentType())
} else {
// let's return an error instead of an nil pointer exception here
return nil, errors.New("TargetType '" + s.TargetType + "' could not be determined")
}
case "":
return nil, errors.New("No method specified")
default:
req, err = http.NewRequest(s.Method, s.Url, nil)
if err != nil {
return nil, err
}
req, err = http.NewRequest(s.Method, s.Url, &buf)
req.Header.Set("Content-Type", mw.FormDataContentType())
} else {
// let's return an error instead of an nil pointer exception here
return nil, errors.New("TargetType '" + s.TargetType + "' could not be determined")
}

for k, v := range s.Header {
Expand Down

0 comments on commit ab5fdf7

Please sign in to comment.