|
|
@@ -1,6 +1,7 @@
|
|
|
package myth
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"compress/gzip"
|
|
|
"crypto/tls"
|
|
|
"crypto/x509"
|
|
|
@@ -35,6 +36,10 @@ const (
|
|
|
|
|
|
// ContentTypeJSON application/json
|
|
|
ContentTypeJSON = `application/json; charset=utf-8`
|
|
|
+ // ContentTypeURL application/x-www-form-urlencoded
|
|
|
+ ContentTypeURL = `application/x-www-form-urlencoded; charset=utf-8`
|
|
|
+ // ContentTypeURL application/xml
|
|
|
+ ContentTypeXML = `application/xml; charset=utf-8`
|
|
|
|
|
|
// RequestTimeOut http request timeout (second)
|
|
|
RequestTimeOut = 30
|
|
|
@@ -103,6 +108,26 @@ func (h *HTTPHeader) Del(k string) {
|
|
|
delete(h.header, k)
|
|
|
}
|
|
|
|
|
|
+// SetContentType set Content-Type
|
|
|
+func (h *HTTPHeader) SetContentType(v string) {
|
|
|
+ h.header[ContentType] = v
|
|
|
+}
|
|
|
+
|
|
|
+// SetContentTypeJSON set Content-Type application/json
|
|
|
+func (h *HTTPHeader) SetContentTypeJSON() {
|
|
|
+ h.header[ContentType] = ContentTypeJSON
|
|
|
+}
|
|
|
+
|
|
|
+// SetContentTypeURL set Content-Type application/x-www-form-urlencoded
|
|
|
+func (h *HTTPHeader) SetContentTypeURL() {
|
|
|
+ h.header[ContentType] = ContentTypeURL
|
|
|
+}
|
|
|
+
|
|
|
+// SetContentTypeXML set Content-Type application/xml
|
|
|
+func (h *HTTPHeader) SetContentTypeXML() {
|
|
|
+ h.header[ContentType] = ContentTypeXML
|
|
|
+}
|
|
|
+
|
|
|
// HTTPReqOption request option
|
|
|
type HTTPReqOption struct {
|
|
|
}
|
|
|
@@ -117,6 +142,26 @@ type HTTPMessage struct {
|
|
|
Res *http.Response
|
|
|
}
|
|
|
|
|
|
+// Headerx return response http.Header
|
|
|
+func (m HTTPMessage) Headerx() http.Header {
|
|
|
+ return m.Header
|
|
|
+}
|
|
|
+
|
|
|
+// Request return raw *http.Request
|
|
|
+func (m HTTPMessage) Request() *http.Request {
|
|
|
+ return m.Req
|
|
|
+}
|
|
|
+
|
|
|
+// Response return raw *http.Response
|
|
|
+func (m HTTPMessage) Response() *http.Response {
|
|
|
+ return m.Res
|
|
|
+}
|
|
|
+
|
|
|
+// ResponseBytes return Response bytes
|
|
|
+func (m HTTPMessage) ResponseBytes() []byte {
|
|
|
+ return m.Body
|
|
|
+}
|
|
|
+
|
|
|
// JSON Body to JSON
|
|
|
func (m HTTPMessage) JSON(dest interface{}) (err error) {
|
|
|
if m.StatusCode != http.StatusOK {
|
|
|
@@ -172,10 +217,6 @@ func newRequest(method, uri, certPath, keyPath string, header map[string]string,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- var (
|
|
|
- client = &http.Client{Transport: t}
|
|
|
- )
|
|
|
-
|
|
|
req, err = http.NewRequest(method, uri, body)
|
|
|
if err != nil {
|
|
|
return
|
|
|
@@ -185,6 +226,7 @@ func newRequest(method, uri, certPath, keyPath string, header map[string]string,
|
|
|
req.Header.Add(k, header[k])
|
|
|
}
|
|
|
|
|
|
+ client := &http.Client{Transport: t}
|
|
|
res, err = client.Do(req)
|
|
|
return
|
|
|
}
|
|
|
@@ -214,6 +256,17 @@ func readBody(msg *HTTPMessage, res *http.Response) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// Postx request with Content-Type
|
|
|
+func Postx(contentType, uri string, headers map[string]string, data []byte) (msg HTTPMessage, err error) {
|
|
|
+ headers[ContentType] = contentType
|
|
|
+ if msg.Req, msg.Res, err = newRequest(http.MethodPost, uri, ``, ``, headers, bytes.NewReader(data)); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer msg.Res.Body.Close()
|
|
|
+ err = readBody(&msg, msg.Res)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
// Post HTTP request POST
|
|
|
func Post(uri, certPath, keyPath string, header map[string]string, data io.Reader) (msg HTTPMessage, err error) {
|
|
|
if msg.Req, msg.Res, err = newRequest(http.MethodPost, uri, certPath, keyPath, header, data); err != nil {
|