2018-10-17 06:25:17 +00:00
|
|
|
package requests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2018-10-26 03:40:04 +00:00
|
|
|
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 {
|
2018-11-21 07:21:01 +00:00
|
|
|
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
|
|
|
}
|
2018-11-21 07:21:01 +00:00
|
|
|
req, err = http.NewRequest(wf.Method, wf.GetRawURL(), bodybuf)
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-11-19 10:07:37 +00:00
|
|
|
if wf.Body.ContentType() != "" {
|
|
|
|
contentType = wf.Body.ContentType()
|
2018-10-25 11:02:55 +00:00
|
|
|
} else {
|
2018-11-19 10:07:37 +00:00
|
|
|
contentType = ""
|
2019-09-05 03:50:53 +00:00
|
|
|
if wf.Method == "POST" || wf.Method == "PUT" || wf.Method == "PATCH" {
|
|
|
|
contentType = TypeURLENCODED
|
2018-10-25 11:02:55 +00:00
|
|
|
}
|
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
|
|
|
|
|
|
|
|
}
|