|
@@ -20,9 +20,6 @@ const (
|
|
|
TypeUrlencoded = "urlencoded"
|
|
TypeUrlencoded = "urlencoded"
|
|
|
TypeForm = "form"
|
|
TypeForm = "form"
|
|
|
TypeFormData = "form-data"
|
|
TypeFormData = "form-data"
|
|
|
- TypeHTML = "html"
|
|
|
|
|
- TypeText = "text"
|
|
|
|
|
- TypeMultipart = "multipart"
|
|
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
var Types = map[string]string{
|
|
var Types = map[string]string{
|
|
@@ -31,9 +28,6 @@ var Types = map[string]string{
|
|
|
TypeForm: "application/x-www-form-urlencoded",
|
|
TypeForm: "application/x-www-form-urlencoded",
|
|
|
TypeFormData: "application/x-www-form-urlencoded",
|
|
TypeFormData: "application/x-www-form-urlencoded",
|
|
|
TypeUrlencoded: "application/x-www-form-urlencoded",
|
|
TypeUrlencoded: "application/x-www-form-urlencoded",
|
|
|
- TypeHTML: "text/html",
|
|
|
|
|
- TypeText: "text/plain",
|
|
|
|
|
- TypeMultipart: "multipart/form-data",
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
|
type Client struct {
|
|
@@ -41,7 +35,7 @@ type Client struct {
|
|
|
Transport *http.Transport
|
|
Transport *http.Transport
|
|
|
Url string
|
|
Url string
|
|
|
Method string
|
|
Method string
|
|
|
- ForceType string
|
|
|
|
|
|
|
+ RequestType string
|
|
|
FormString string
|
|
FormString string
|
|
|
ContentType string
|
|
ContentType string
|
|
|
UnmarshalType string
|
|
UnmarshalType string
|
|
@@ -57,7 +51,7 @@ func NewHttpClient() (client *Client) {
|
|
|
c.HttpClient = new(http.Client)
|
|
c.HttpClient = new(http.Client)
|
|
|
c.Transport = &http.Transport{}
|
|
c.Transport = &http.Transport{}
|
|
|
c.Transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
c.Transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
- c.ForceType = TypeUrlencoded
|
|
|
|
|
|
|
+ c.RequestType = TypeUrlencoded
|
|
|
c.UnmarshalType = TypeJSON
|
|
c.UnmarshalType = TypeJSON
|
|
|
c.Errors = make([]error, 0)
|
|
c.Errors = make([]error, 0)
|
|
|
return c
|
|
return c
|
|
@@ -81,7 +75,7 @@ func (c *Client) Post(url string) (client *Client) {
|
|
|
func (c *Client) Type(typeStr string) (client *Client) {
|
|
func (c *Client) Type(typeStr string) (client *Client) {
|
|
|
if _, ok := Types[typeStr]; ok {
|
|
if _, ok := Types[typeStr]; ok {
|
|
|
c.mu.Lock()
|
|
c.mu.Lock()
|
|
|
- c.ForceType = typeStr
|
|
|
|
|
|
|
+ c.RequestType = typeStr
|
|
|
c.mu.Unlock()
|
|
c.mu.Unlock()
|
|
|
} else {
|
|
} else {
|
|
|
c.Errors = append(c.Errors, errors.New("Type func: incorrect type \""+typeStr+"\""))
|
|
c.Errors = append(c.Errors, errors.New("Type func: incorrect type \""+typeStr+"\""))
|
|
@@ -149,7 +143,7 @@ func (c *Client) EndBytes() (res *http.Response, bs []byte, errs []error) {
|
|
|
if len(c.Errors) > 0 {
|
|
if len(c.Errors) > 0 {
|
|
|
return nil, nil, c.Errors
|
|
return nil, nil, c.Errors
|
|
|
}
|
|
}
|
|
|
- var reader *strings.Reader
|
|
|
|
|
|
|
+ var reader = strings.NewReader(null)
|
|
|
|
|
|
|
|
req, err := func() (*http.Request, error) {
|
|
req, err := func() (*http.Request, error) {
|
|
|
c.mu.RLock()
|
|
c.mu.RLock()
|
|
@@ -157,9 +151,9 @@ func (c *Client) EndBytes() (res *http.Response, bs []byte, errs []error) {
|
|
|
|
|
|
|
|
switch c.Method {
|
|
switch c.Method {
|
|
|
case GET:
|
|
case GET:
|
|
|
- reader = strings.NewReader(null)
|
|
|
|
|
|
|
+ //todo: nothing
|
|
|
case POST:
|
|
case POST:
|
|
|
- switch c.ForceType {
|
|
|
|
|
|
|
+ switch c.RequestType {
|
|
|
case TypeJSON:
|
|
case TypeJSON:
|
|
|
if c.JsonByte != nil {
|
|
if c.JsonByte != nil {
|
|
|
reader = strings.NewReader(string(c.JsonByte))
|
|
reader = strings.NewReader(string(c.JsonByte))
|
|
@@ -173,10 +167,10 @@ func (c *Client) EndBytes() (res *http.Response, bs []byte, errs []error) {
|
|
|
c.ContentType = Types[TypeXML]
|
|
c.ContentType = Types[TypeXML]
|
|
|
c.UnmarshalType = TypeXML
|
|
c.UnmarshalType = TypeXML
|
|
|
default:
|
|
default:
|
|
|
- c.Errors = append(c.Errors, errors.New("Request type Error "))
|
|
|
|
|
|
|
+ return nil, errors.New("Request type Error ")
|
|
|
}
|
|
}
|
|
|
default:
|
|
default:
|
|
|
- reader = strings.NewReader(null)
|
|
|
|
|
|
|
+ return nil, errors.New("Only support Get and Post ")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(c.Method, c.Url, reader)
|
|
req, err := http.NewRequest(c.Method, c.Url, reader)
|