requests/base.go
2018-10-17 14:25:17 +08:00

43 lines
819 B
Go

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