requests/base.go

52 lines
1.0 KiB
Go
Raw Normal View History

2018-10-17 06:25:17 +00:00
package requests
import (
"bytes"
"errors"
"net/http"
"reflect"
)
func buildBodyRequest(wf *Workflow) *http.Request {
2018-10-17 06:25:17 +00:00
var req *http.Request
var err error
2018-10-25 10:45:46 +00:00
contentType := ""
2018-10-17 06:25:17 +00:00
if wf.Body.IOBody() == nil {
req, err = http.NewRequest(wf.Method, wf.GetStringURL(), nil)
2018-10-17 06:25:17 +00:00
} else {
2018-10-23 09:08:50 +00:00
var bodybuf *bytes.Buffer
switch wf.Body.IOBody().(type) {
2018-10-17 06:25:17 +00:00
case []byte:
bodybuf = bytes.NewBuffer(wf.Body.IOBody().([]byte))
2018-10-17 06:25:17 +00:00
case *bytes.Buffer:
bodybuf = bytes.NewBuffer(wf.Body.IOBody().(*bytes.Buffer).Bytes())
2018-10-17 06:25:17 +00:00
default:
panic(errors.New("the type is not exist, type is" + reflect.TypeOf(wf.Body.IOBody).String()))
2018-10-17 06:25:17 +00:00
}
req, err = http.NewRequest(wf.Method, wf.GetStringURL(), bodybuf)
2018-10-17 06:25:17 +00:00
}
if err != nil {
panic(err)
}
if wf.Body.ContentType() != "" {
contentType = wf.Body.ContentType()
} else {
contentType = ""
if contentType == "" {
if wf.Method == "POST" || wf.Method == "PUT" || wf.Method == "PATCH" {
contentType = TypeURLENCODED
}
}
2018-10-25 10:45:46 +00:00
}
if contentType != "" {
req.Header.Set(HeaderKeyContentType, contentType)
2018-10-17 06:25:17 +00:00
}
return req
}