requests/base.go
2018-10-25 18:45:46 +08:00

46 lines
884 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
contentType := ""
if body.IOBody == nil {
req, err = http.NewRequest(ver, rawurl, nil)
} else {
contentType = TypeURLENCODED
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 != "" {
contentType = body.ContentType
}
if contentType != "" {
req.Header.Set(HeaderKeyContentType, contentType)
}
return req
}