requests/base.go

42 lines
837 B
Go
Raw Normal View History

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