requests/base.go

52 lines
1.1 KiB
Go
Raw Permalink 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
2018-11-21 08:29:13 +00:00
if wf.Body.GetIOBody() == nil {
req, err = http.NewRequest(wf.Method, wf.GetRawURL(), nil)
2018-10-17 06:25:17 +00:00
} else {
2018-10-23 09:08:50 +00:00
var bodybuf *bytes.Buffer
2018-11-21 08:29:13 +00:00
switch wf.Body.GetIOBody().(type) {
2018-10-17 06:25:17 +00:00
case []byte:
2018-11-21 08:29:13 +00:00
bodybuf = bytes.NewBuffer(wf.Body.GetIOBody().([]byte))
2018-11-23 07:45:34 +00:00
case string:
bodybuf = bytes.NewBuffer([]byte(wf.Body.GetIOBody().(string)))
2018-10-17 06:25:17 +00:00
case *bytes.Buffer:
2018-11-21 08:29:13 +00:00
bodybuf = bytes.NewBuffer(wf.Body.GetIOBody().(*bytes.Buffer).Bytes())
2018-10-17 06:25:17 +00:00
default:
2018-11-23 07:45:34 +00:00
panic(errors.New("the type is not exist, type is " + reflect.TypeOf(wf.Body.GetIOBody()).String()))
2018-10-17 06:25:17 +00:00
}
req, err = http.NewRequest(wf.Method, wf.GetRawURL(), 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 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
}