MCPcopy Create free account
hub / github.com/imroc/req / SetFile

Method SetFile

request.go:301–329  ·  view source on GitHub ↗

SetFile set up a multipart form from file path to upload, which read file from filePath automatically to upload.

(paramName, filePath string)

Source from the content-addressed store, hash-verified

299// SetFile set up a multipart form from file path to upload,
300// which read file from filePath automatically to upload.
301func (r *Request) SetFile(paramName, filePath string) *Request {
302 file, err := os.Open(filePath)
303 if err != nil {
304 r.client.log.Errorf("failed to open %s: %v", filePath, err)
305 r.appendError(err)
306 return r
307 }
308 fileInfo, err := os.Stat(filePath)
309 if err != nil {
310 r.client.log.Errorf("failed to stat file %s: %v", filePath, err)
311 r.appendError(err)
312 return r
313 }
314 r.isMultiPart = true
315 return r.SetFileUpload(FileUpload{
316 ParamName: paramName,
317 FileName: filepath.Base(filePath),
318 GetFileContent: func() (io.ReadCloser, error) {
319 if r.RetryAttempt > 0 {
320 file, err = os.Open(filePath)
321 if err != nil {
322 return nil, err
323 }
324 }
325 return file, nil
326 },
327 FileSize: fileInfo.Size(),
328 })
329}
330
331var (
332 errMissingParamName = errors.New("missing param name in multipart file upload")

Callers 7

SetFilesMethod · 0.95
SetFileFunction · 0.80
TestUploadMultipartFunction · 0.80
TestSetFileWithRetryFunction · 0.80
TestSetFileFunction · 0.80
TestUploadCallbackFunction · 0.80
mainFunction · 0.80

Calls 3

appendErrorMethod · 0.95
SetFileUploadMethod · 0.95
ErrorfMethod · 0.65

Tested by 4

TestUploadMultipartFunction · 0.64
TestSetFileWithRetryFunction · 0.64
TestSetFileFunction · 0.64
TestUploadCallbackFunction · 0.64