Browse Source

增加发送xml

master v0.4.0
guzeng 2 years ago
parent
commit
2e9178a53e
2 changed files with 41 additions and 1 deletions
  1. +40
    -0
      http.go
  2. +1
    -1
      url.go

+ 40
- 0
http.go View File

@ -84,3 +84,43 @@ func SendHttp(method, url string, param map[string]string, header ...map[string]
return body, nil return body, nil
} }
/**
* send xml
*/
func SendXml(method, url string, requestxml interface{}, header ...map[string]string) ([]byte, error) {
bytexml, err := xml.Marshal(&requestxml)
if err != nil {
return []byte(""), err
}
httpClient := &http.Client{}
req, err := http.NewRequest(method, url, bytes.NewBuffer(param))
if err != nil {
return []byte(""), err
}
req.Header.Add("Content-Type", "application/xml; charset=utf-8")
if len(header) > 0 {
for _, item := range header {
for k, v := range item {
req.Header.Add(k, v)
}
}
}
resp, err := httpClient.Do(req)
if err != nil {
return []byte(""), err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte(""), err
}
return body, nil
}

+ 1
- 1
url.go View File

@ -100,7 +100,7 @@ func PostJsonData(url string, param interface{}, header ...map[string]string) ([
if len(header) > 0 { if len(header) > 0 {
for _, item := range header { for _, item := range header {
for k, v := range item { for k, v := range item {
req.Header.Set(k, v)
req.Header[k] = []string{v}
} }
} }
} }


Loading…
Cancel
Save