requests/base.go
huangsimin 9750935138 v0.0.5
2018-10-23 17:08:50 +08:00

42 lines
843 B
Go

package requests
import (
"bytes"
"errors"
"net/http"
"reflect"
)
func buildBodyRequest(ver, rawurl string, body *Body) *http.Request {
var req *http.Request
var err error
if body.IOBody == nil {
req, err = http.NewRequest(ver, rawurl, nil)
} else {
var bodybuf *bytes.Buffer
switch body.IOBody.(type) {
case []byte:
bodybuf = bytes.NewBuffer(body.IOBody.([]byte))
case *bytes.Buffer:
bodybuf = bytes.NewBuffer(body.IOBody.(*bytes.Buffer).Bytes())
default:
panic(errors.New("the type is not exist, type is" + reflect.TypeOf(body.IOBody).String()))
}
req, err = http.NewRequest(ver, rawurl, bodybuf)
}
if err != nil {
panic(err)
}
if body.ContentType == "" {
req.Header.Set(HeaderKeyContentType, TypeURLENCODED)
} else {
req.Header.Set(HeaderKeyContentType, body.ContentType)
}
return req
}