This function creates a new HTTP request for file upload. It takes in the URI of the server, the authentication key, parameters, the name of the parameter, and the path of the file. It returns the HTTP request and any error that occurred.
(uri string, authKey string, params map[string]string, paramName, path string)
| 168 | // It takes in the URI of the server, the authentication key, parameters, the name of the parameter, and the path of the file. |
| 169 | // It returns the HTTP request and any error that occurred. |
| 170 | func newfileUploadRequest(uri string, authKey string, params map[string]string, paramName, path string) (*http.Request, error) { |
| 171 | file, err := os.Open(path) |
| 172 | if err != nil { |
| 173 | return nil, err |
| 174 | } |
| 175 | defer file.Close() |
| 176 | |
| 177 | body := &bytes.Buffer{} |
| 178 | writer := multipart.NewWriter(body) |
| 179 | part, err := writer.CreateFormFile(paramName, path) |
| 180 | if err != nil { |
| 181 | return nil, err |
| 182 | } |
| 183 | _, err = io.Copy(part, file) |
| 184 | if err != nil { |
| 185 | return nil, err |
| 186 | } |
| 187 | for key, val := range params { |
| 188 | _ = writer.WriteField(key, val) |
| 189 | } |
| 190 | err = writer.Close() |
| 191 | if err != nil { |
| 192 | return nil, err |
| 193 | } |
| 194 | request, err := http.NewRequest("POST", uri, body) |
| 195 | request.Header.Set("Content-Type", writer.FormDataContentType()) |
| 196 | if authKey != "" { |
| 197 | request.Header.Set("x-auth-key", authKey) |
| 198 | } |
| 199 | return request, err |
| 200 | } |
| 201 | |
| 202 | func executeCommand(cmd *exec.Cmd) error { |
| 203 | stdout, err := cmd.StdoutPipe() |
no outgoing calls
no test coverage detected