requests/base.go
2018-10-17 18:38:04 +08:00

42 lines
837 B
Go

package requests
import (
"bytes"
"errors"
"net/http"
"reflect"
)
func buildBodyRequest(ver, rawurl string, params *Params) *http.Request {
var req *http.Request
var err error
if params.IOBody == nil {
req, err = http.NewRequest(ver, rawurl, nil)
} else {
var body *bytes.Buffer
switch params.IOBody.(type) {
case []byte:
body = bytes.NewBuffer(params.IOBody.([]byte))
case *bytes.Buffer:
body = bytes.NewBuffer(params.IOBody.(*bytes.Buffer).Bytes())
default:
panic(errors.New("the type is not exist, type is" + reflect.TypeOf(params.IOBody).String()))
}
req, err = http.NewRequest(ver, rawurl, 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
}