Fixed Content-Type in mime/multipart CreateFormFile: Why?

278 views
Skip to first unread message

Volker Dobler

unread,
Nov 11, 2011, 3:14:02 AM11/11/11
to golang-nuts
Is there a special reason why CreateFormFile() in package multipart
uses a fixed Content-Type of "application/octet-stream"?

I think this makes the "convenience wrapper around CreatePart"
very inconvenient as you cannot use it if you need a different
content type. Making the content type selectable by the user
would be straight forward and would still be convenient but more
flexible:

func (w *Writer) CreateFormFile(fieldname, filename, contentType
string) (io.Writer, os.Error) {
if contentType == "" {
contentType == "application/octet-stream"
}
...
}

// same result as with existing code:
CreateFormFile(field, filename, "")

// two (okay four) more lines of code to be more specific:
ext := ""
if i:=string.LastIndex(filename, "."); i!=.1 { ext = filename[i:] }
CreateFormFile(field, filename, mime.TypeByExtension(ext))

Regards, Volker

Brad Fitzpatrick

unread,
Nov 11, 2011, 12:26:50 PM11/11/11
to Volker Dobler, golang-nuts
We should remove the word convenient.  That's superfluous and/or subjective.

If you don't find it convenient, why don't you use CreatePart?  It's not much code.

We could add 10 parameters to CreateFormFile for everything people might ever need, but then most of them would be "" or nil.  I thought 2 was the sweet spot.  You argue for 3.  I don't have data to suggest either of us is necessarily right as to what most callers would expect.  *shrug*  You could imagine that mime type being sniffed and set automatically, using http.DetectContentType (which is based off of http://mimesniff.spec.whatwg.org/) if we fixed the circular dependency and moved sniffing to be a leaf package.
Reply all
Reply to author
Forward
0 new messages