requests/base.go

52 lines
1.0 KiB
Go

package requests
import (
"bytes"
"errors"
"net/http"
"reflect"
)
func buildBodyRequest(wf *Workflow) *http.Request {
var req *http.Request
var err error
contentType := ""
if wf.Body.IOBody() == nil {
req, err = http.NewRequest(wf.Method, wf.GetStringURL(), nil)
} else {
var bodybuf *bytes.Buffer
switch wf.Body.IOBody().(type) {
case []byte:
bodybuf = bytes.NewBuffer(wf.Body.IOBody().([]byte))
case *bytes.Buffer:
bodybuf = bytes.NewBuffer(wf.Body.IOBody().(*bytes.Buffer).Bytes())
default:
panic(errors.New("the type is not exist, type is" + reflect.TypeOf(wf.Body.IOBody).String()))
}
req, err = http.NewRequest(wf.Method, wf.GetStringURL(), bodybuf)
}
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
}
}
}
if contentType != "" {
req.Header.Set(HeaderKeyContentType, contentType)
}
return req
}