2018-10-17 06:25:17 +00:00
|
|
|
package requests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2018-10-23 09:08:50 +00:00
|
|
|
func buildBodyRequest(ver, rawurl string, body *Body) *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-10-23 09:08:50 +00:00
|
|
|
if body.IOBody == nil {
|
2018-10-17 10:38:04 +00:00
|
|
|
req, err = http.NewRequest(ver, rawurl, nil)
|
2018-10-17 06:25:17 +00:00
|
|
|
} else {
|
2018-10-25 10:45:46 +00:00
|
|
|
contentType = TypeURLENCODED
|
2018-10-23 09:08:50 +00:00
|
|
|
var bodybuf *bytes.Buffer
|
|
|
|
switch body.IOBody.(type) {
|
2018-10-17 06:25:17 +00:00
|
|
|
case []byte:
|
2018-10-23 09:08:50 +00:00
|
|
|
bodybuf = bytes.NewBuffer(body.IOBody.([]byte))
|
2018-10-17 06:25:17 +00:00
|
|
|
case *bytes.Buffer:
|
2018-10-23 09:08:50 +00:00
|
|
|
bodybuf = bytes.NewBuffer(body.IOBody.(*bytes.Buffer).Bytes())
|
2018-10-17 06:25:17 +00:00
|
|
|
default:
|
2018-10-23 09:08:50 +00:00
|
|
|
panic(errors.New("the type is not exist, type is" + reflect.TypeOf(body.IOBody).String()))
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
2018-10-23 09:08:50 +00:00
|
|
|
req, err = http.NewRequest(ver, rawurl, bodybuf)
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-10-25 10:45:46 +00:00
|
|
|
if body.ContentType != "" {
|
|
|
|
contentType = body.ContentType
|
|
|
|
}
|
|
|
|
|
|
|
|
if contentType != "" {
|
|
|
|
req.Header.Set(HeaderKeyContentType, contentType)
|
2018-10-17 06:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return req
|
|
|
|
|
|
|
|
}
|