Add HttpPost
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
import (
|
||||
// "crypto/md5"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"crypto/tls"
|
||||
// "os"
|
||||
"io"
|
||||
// "fmt"
|
||||
@@ -19,7 +21,7 @@
|
||||
"github.com/andybalholm/brotli"
|
||||
)
|
||||
|
||||
func (app *App) HttpGet(url string) (map[string]interface{}) {
|
||||
func (app *App) HttpPost(url string, head map[string]string, cookie []http.Cookie, data url.Values) (map[string]interface{}) {
|
||||
ret := map[string]interface{}{
|
||||
"Error" : nil ,
|
||||
"RequestUrl" : url ,
|
||||
@@ -32,9 +34,17 @@
|
||||
"Content-Length" : "" ,
|
||||
}
|
||||
|
||||
app.Log(url)
|
||||
app.Log("%s", url)
|
||||
|
||||
cli := &http.Client{}
|
||||
var tr *http.Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
|
||||
}
|
||||
|
||||
if !app.HttpCliSslVerify {
|
||||
tr.TLSClientConfig.InsecureSkipVerify = true
|
||||
}
|
||||
|
||||
cli := &http.Client{Transport: tr}
|
||||
|
||||
ifcRawHead, err := app.ConfGetIfc("httpTest", "head")
|
||||
|
||||
@@ -52,10 +62,33 @@
|
||||
mapStrHead[key] = []string{ val.(string) }
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
// app.Log(head)
|
||||
|
||||
|
||||
for key, val := range head {
|
||||
mapStrHead[key] = []string{ val }
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(
|
||||
"POST",
|
||||
url,
|
||||
strings.NewReader(data.Encode()),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
app.Log(err)
|
||||
ret["Error"] = err
|
||||
return ret
|
||||
}
|
||||
|
||||
req.Header = mapStrHead
|
||||
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
for _, coo := range cookie {
|
||||
req.AddCookie(&coo)
|
||||
}
|
||||
|
||||
res, err := cli.Do(req)
|
||||
|
||||
if err != nil {
|
||||
@@ -75,6 +108,14 @@
|
||||
ret["StatusCode" ] = res.StatusCode
|
||||
ret["Content-Length"] = res.ContentLength
|
||||
ret["Header" ] = res.Header
|
||||
ret["Cookies" ] = res.Cookies()
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
app.Log("StatusCode != 200")
|
||||
app.Log(req)
|
||||
app.Log(req.Header)
|
||||
app.Log(mapStrHead)
|
||||
}
|
||||
|
||||
// app.Log("Response status: %s", res.Status)
|
||||
// app.Log("Header: %+v", res.Header)
|
||||
@@ -132,7 +173,175 @@
|
||||
return ret
|
||||
}
|
||||
|
||||
app.Log("Read %s", size)
|
||||
if app.HttpCliDebug > 1 {
|
||||
app.Log("Read %s", size)
|
||||
}
|
||||
|
||||
var contRaw []byte = out.Bytes()
|
||||
ret["ContentRaw"] = contRaw
|
||||
|
||||
if ret["Content-Type"] == "application/json" {
|
||||
var contJson interface{}
|
||||
|
||||
err = json.Unmarshal(contRaw, &contJson)
|
||||
|
||||
if err != nil {
|
||||
app.Log(err)
|
||||
ret["Error"] = err
|
||||
return ret
|
||||
}
|
||||
|
||||
ret["ContentJson"] = contJson
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
|
||||
func (app *App) HttpGet(url string, head map[string]string, cookie []http.Cookie) (map[string]interface{}) {
|
||||
ret := map[string]interface{}{
|
||||
"Error" : nil ,
|
||||
"RequestUrl" : url ,
|
||||
"StatusCode" : 0 ,
|
||||
"ContentRaw" : nil ,
|
||||
"ContentJson" : nil ,
|
||||
"Header" : nil ,
|
||||
"Content-Encoding" : "" ,
|
||||
"Content-Type" : "" ,
|
||||
"Content-Length" : "" ,
|
||||
}
|
||||
|
||||
app.Log("%s", url)
|
||||
|
||||
var tr *http.Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
|
||||
}
|
||||
|
||||
if !app.HttpCliSslVerify {
|
||||
tr.TLSClientConfig.InsecureSkipVerify = true
|
||||
}
|
||||
|
||||
cli := &http.Client{Transport: tr}
|
||||
|
||||
ifcRawHead, err := app.ConfGetIfc("httpTest", "head")
|
||||
|
||||
if err != nil {
|
||||
app.Log(err)
|
||||
ret["Error"] = err
|
||||
return ret
|
||||
}
|
||||
|
||||
mapIfcHead := ifcRawHead.(map[string]interface{})
|
||||
|
||||
mapStrHead := make(map[string][]string)
|
||||
|
||||
for key, val := range mapIfcHead {
|
||||
mapStrHead[key] = []string{ val.(string) }
|
||||
}
|
||||
|
||||
|
||||
// app.Log(head)
|
||||
|
||||
|
||||
for key, val := range head {
|
||||
mapStrHead[key] = []string{ val }
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
|
||||
req.Header = mapStrHead
|
||||
|
||||
for _, coo := range cookie {
|
||||
req.AddCookie(&coo)
|
||||
}
|
||||
|
||||
res, err := cli.Do(req)
|
||||
|
||||
if err != nil {
|
||||
app.Log(err)
|
||||
ret["Error"] = err
|
||||
return ret
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
if err != nil {
|
||||
app.Log(err)
|
||||
ret["Error"] = err
|
||||
return ret
|
||||
}
|
||||
|
||||
ret["StatusCode" ] = res.StatusCode
|
||||
ret["Content-Length"] = res.ContentLength
|
||||
ret["Header" ] = res.Header
|
||||
ret["Cookies" ] = res.Cookies()
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
app.Log("StatusCode != 200")
|
||||
app.Log(req)
|
||||
app.Log(req.Header)
|
||||
app.Log(mapStrHead)
|
||||
}
|
||||
|
||||
// app.Log("Response status: %s", res.Status)
|
||||
// app.Log("Header: %+v", res.Header)
|
||||
|
||||
// app.Log("TransferEncoding: %+v", res.TransferEncoding)
|
||||
// app.Log("ContentLength: %+v", res.ContentLength)
|
||||
|
||||
var tmpl []string
|
||||
|
||||
tmpl = res.Header["Content-Encoding"]
|
||||
if len(tmpl) > 0 {
|
||||
ret["Content-Encoding"] = tmpl[0]
|
||||
}
|
||||
|
||||
tmpl = res.Header["Content-Type"]
|
||||
if len(tmpl) > 0 {
|
||||
tmpl = strings.Split(tmpl[0], ";")
|
||||
|
||||
if len(tmpl) > 0 {
|
||||
ret["Content-Type"] = tmpl[0]
|
||||
}
|
||||
}
|
||||
|
||||
// Content-Disposition: attachment; name="fieldName"; filename="myfile.txt"
|
||||
|
||||
|
||||
out := new(bytes.Buffer)
|
||||
var encReader io.Reader
|
||||
|
||||
switch ret["Content-Encoding"] {
|
||||
case "":
|
||||
encReader = bufio.NewReader(res.Body)
|
||||
case "br":
|
||||
encReader = brotli.NewReader(res.Body)
|
||||
case "gzip":
|
||||
encReader, err = gzip.NewReader(res.Body)
|
||||
case "deflate":
|
||||
encReader = flate.NewReader(res.Body)
|
||||
default:
|
||||
app.Log("Unknown Content-Encoding %s", ret["Content-Encoding"])
|
||||
encReader = bufio.NewReader(res.Body)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
app.Log(err)
|
||||
ret["Error"] = err
|
||||
return ret
|
||||
}
|
||||
|
||||
size, err := out.ReadFrom(encReader)
|
||||
|
||||
if err != nil {
|
||||
app.Log(err)
|
||||
ret["Error"] = err
|
||||
return ret
|
||||
}
|
||||
|
||||
if app.HttpCliDebug > 1 {
|
||||
app.Log("Read %s", size)
|
||||
}
|
||||
|
||||
var contRaw []byte = out.Bytes()
|
||||
ret["ContentRaw"] = contRaw
|
||||
@@ -178,7 +387,7 @@
|
||||
go func() {
|
||||
var a map[string]interface{}
|
||||
|
||||
a = app.HttpGet(url)
|
||||
a = app.HttpGet(url, nil, nil)
|
||||
|
||||
app.Log(a)
|
||||
|
||||
@@ -194,3 +403,4 @@
|
||||
"request": url,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user