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

Function PostFile

build-in-package/http/http.go:147–180  ·  view source on GitHub ↗
(uri string, params map[string]string, paramName, path string)

Source from the content-addressed store, hash-verified

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 {
150 return nil, err
151 }
152 defer file.Close()
153 body := &bytes.Buffer{}
154 writer := multipart.NewWriter(body)
155 part, err := writer.CreateFormFile(paramName, filepath.Base(path))
156 if err != nil {
157 return nil, err
158 }
159 _, err = io.Copy(part, file)
160
161 for key, val := range params {
162 _ = writer.WriteField(key, val)
163 }
164 err = writer.Close()
165 if err != nil {
166 return nil, err
167 }
168
169 req, _ := http.NewRequest("POST", uri, body)
170 client := newHTTPClient()
171 res, resErr := client.Do(req)
172 if resErr != nil {
173 return nil, err
174 }
175 data, errRead := ioutil.ReadAll(res.Body)
176 if errRead != nil {
177 return nil, errRead
178 }
179 return data, nil
180}
181func HeartHandle(write http.ResponseWriter, request *http.Request) {
182 _, _ = write.Write([]byte(request.URL.Path))
183 return

Callers 1

TestPostFileFunction · 0.85

Calls 4

newHTTPClientFunction · 0.85
CopyMethod · 0.80
DoMethod · 0.65
CloseMethod · 0.45

Tested by 1

TestPostFileFunction · 0.68