@@ -734,10 +734,11 @@ func (s *SuperAgent) SendString(content string) *SuperAgent {
734734type File struct {
735735 Filename string
736736 Fieldname string
737+ MimeType string
737738 Data []byte
738739}
739740
740- // SendFile function works only with type "multipart". The function accepts one mandatory and up to two optional arguments. The mandatory (first) argument is the file.
741+ // SendFile function works only with type "multipart". The function accepts one mandatory and up to three optional arguments. The mandatory (first) argument is the file.
741742// The function accepts a path to a file as string:
742743//
743744// gorequest.New().
@@ -784,10 +785,20 @@ type File struct {
784785// SendFile(b, "", "my_custom_fieldname"). // filename left blank, will become "example_file.ext"
785786// End()
786787//
788+ // The third optional argument (fourth argument overall) is the mimetype request form-data part. It defaults to "application/octet-stream".
789+ //
790+ // b, _ := ioutil.ReadFile("./example_file.ext")
791+ // gorequest.New().
792+ // Post("http://example.com").
793+ // Type("multipart").
794+ // SendFile(b, "filename", "my_custom_fieldname", "mime_type").
795+ // End()
796+ //
787797func (s * SuperAgent ) SendFile (file interface {}, args ... string ) * SuperAgent {
788798
789799 filename := ""
790800 fieldname := "file"
801+ fileType := "application/octet-stream"
791802
792803 if len (args ) >= 1 && len (args [0 ]) > 0 {
793804 filename = strings .TrimSpace (args [0 ])
@@ -798,6 +809,13 @@ func (s *SuperAgent) SendFile(file interface{}, args ...string) *SuperAgent {
798809 if fieldname == "file" || fieldname == "" {
799810 fieldname = "file" + strconv .Itoa (len (s .FileData )+ 1 )
800811 }
812+ if len (args ) >= 3 && len (args [2 ]) > 0 {
813+ fileType = strings .TrimSpace (args [2 ])
814+ }
815+ if fileType == "" {
816+ s .Errors = append (s .Errors , errors .New ("The fourth SendFile method argument for MIME type cannot be an empty string" ))
817+ return s
818+ }
801819
802820 switch v := reflect .ValueOf (file ); v .Kind () {
803821 case reflect .String :
@@ -817,6 +835,7 @@ func (s *SuperAgent) SendFile(file interface{}, args ...string) *SuperAgent {
817835 s .FileData = append (s .FileData , File {
818836 Filename : filename ,
819837 Fieldname : fieldname ,
838+ MimeType : fileType ,
820839 Data : data ,
821840 })
822841 case reflect .Slice :
@@ -827,6 +846,7 @@ func (s *SuperAgent) SendFile(file interface{}, args ...string) *SuperAgent {
827846 f := File {
828847 Filename : filename ,
829848 Fieldname : fieldname ,
849+ MimeType : fileType ,
830850 Data : make ([]byte , len (slice )),
831851 }
832852 for i := range slice {
@@ -837,9 +857,12 @@ func (s *SuperAgent) SendFile(file interface{}, args ...string) *SuperAgent {
837857 if len (args ) == 1 {
838858 return s .SendFile (v .Elem ().Interface (), args [0 ])
839859 }
840- if len (args ) > = 2 {
860+ if len (args ) = = 2 {
841861 return s .SendFile (v .Elem ().Interface (), args [0 ], args [1 ])
842862 }
863+ if len (args ) >= 3 {
864+ return s .SendFile (v .Elem ().Interface (), args [0 ], args [1 ], args [2 ])
865+ }
843866 return s .SendFile (v .Elem ().Interface ())
844867 default :
845868 if v .Type () == reflect .TypeOf (os.File {}) {
@@ -855,6 +878,7 @@ func (s *SuperAgent) SendFile(file interface{}, args ...string) *SuperAgent {
855878 s .FileData = append (s .FileData , File {
856879 Filename : filename ,
857880 Fieldname : fieldname ,
881+ MimeType : fileType ,
858882 Data : data ,
859883 })
860884 return s
@@ -1239,7 +1263,7 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) {
12391263 // add the files
12401264 if len (s .FileData ) != 0 {
12411265 for _ , file := range s .FileData {
1242- fw , _ := mw . CreateFormFile (file .Fieldname , file .Filename )
1266+ fw , _ := CreateFormFile (mw , file .Fieldname , file .Filename , file . MimeType )
12431267 fw .Write (file .Data )
12441268 }
12451269 contentReader = buf
@@ -1299,6 +1323,23 @@ func (s *SuperAgent) MakeRequest() (*http.Request, error) {
12991323 return req , nil
13001324}
13011325
1326+ var quoteEscaper = strings .NewReplacer ("\\ " , "\\ \\ " , `"` , "\\ \" " )
1327+
1328+ func escapeQuotes (s string ) string {
1329+ return quoteEscaper .Replace (s )
1330+ }
1331+
1332+ // CreateFormFile is a convenience wrapper around CreatePart. It creates
1333+ // a new form-data header with the provided field name and file name.
1334+ func CreateFormFile (w * multipart.Writer , fieldname , filename string , contenttype string ) (io.Writer , error ) {
1335+ h := make (textproto.MIMEHeader )
1336+ h .Set ("Content-Disposition" ,
1337+ fmt .Sprintf (`form-data; name="%s"; filename="%s"` ,
1338+ escapeQuotes (fieldname ), escapeQuotes (filename )))
1339+ h .Set ("Content-Type" , contenttype )
1340+ return w .CreatePart (h )
1341+ }
1342+
13021343// AsCurlCommand returns a string representing the runnable `curl' command
13031344// version of the request.
13041345func (s * SuperAgent ) AsCurlCommand () (string , error ) {
0 commit comments