MCPcopy
hub / github.com/betty200744/ultimate-go / DoPost

Function DoPost

build-in-package/http/http.go:109–146  ·  view source on GitHub ↗
(formType int, url string, header map[string]string, body []byte)

Source from the content-addressed store, hash-verified

107 return ioutil.ReadAll(res.Body)
108}
109func DoPost(formType int, url string, header map[string]string, body []byte) (bs []byte, err error) {
110 var req *http.Request
111 req, err = http.NewRequest("POST", url, bytes.NewReader(body))
112 if err != nil {
113 return
114 }
115 if header == nil {
116 if formType == TypeXml {
117 req.Header.Set("Accept", "application/xml")
118 req.Header.Set("Content-Type", "application/xml;charset=utf-8")
119 } else if formType == TypeForm {
120 req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
121 } else if formType == TypeJson {
122 req.Header.Set("Content-Type", "application/json;charset=utf-8")
123 }
124 } else {
125 for k, v := range header {
126 req.Header.Set(k, v)
127 }
128 }
129
130 var resp *http.Response
131 c := http.Client{}
132 resp, err = c.Do(req)
133 if err != nil {
134 return
135 }
136 defer resp.Body.Close()
137 if resp.StatusCode != 200 {
138 err = fmt.Errorf("StatusCode=%d", resp.StatusCode)
139 return
140 }
141 bs, err = ioutil.ReadAll(resp.Body)
142 if err != nil {
143 return
144 }
145 return
146}
147func PostFile(uri string, params map[string]string, paramName, path string) ([]byte, error) {
148 file, err := os.Open(path)
149 if err != nil {

Callers 1

TestDoPostFunction · 0.85

Calls 3

SetMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.45

Tested by 1

TestDoPostFunction · 0.68