New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: mime/multipart: allow specifying content type in Writer.CreateFormFile #49329
Comments
Perhaps a |
@neild ok, so what are my next steps to make a proposal? |
I added a proposal prefix to the issue, this should be legit according to the docs. |
No. |
Concretizing the proposal:
|
@neild what do you think about options pattern for this method? Just to add additional third argument with options like this: type Option = func(h *textproto.MIMEHeader)
var WithContentType Option = ...
var WithContentRanges Option = ...
func (w *Writer) CreateFormFile(fieldname, filename string, options ...Option) (io.Writer, error) This allows to set additional headers like |
Seems heavyweight for this case. If we want a more general-purpose helper function, then perhaps something like this would be useful:
Usage:
|
@neild seems reasonable. Interesting also to hear other opinions. |
This looks like a duplicate of #46771 ? |
This proposal has been added to the active column of the proposals project |
With #46771 accepted, this use case becomes:
Does that seem OK? It seems clearer and more extensible to do that than to have another constructor for each header we might want to set. |
@rsc looks good to me. What do you think about idea to introduce constants for formdata-common headers like h := make(textproto.MIMEHeader)
h.Set(multipartHeader.ContentDisposition, FileContentDisposition(fieldname, filename)
h.Set(multipart.HeaderContentType, "application/pdf")
p, err := w.CreatePart(h) |
We have avoided adding "Content-Type" as a named constant in net/http, so it doesn't seem like we should add it here either. We did reluctantly add things like Given that #46771 takes care of this use case, closing as duplicate. |
This proposal is a duplicate of a previously discussed proposal, as noted above, |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I'm using
multipart.Writer
to build request for uploading a file.I had a case in which I need to set an appropriate content type for uploaded file in order to submit a correct request.
Unfortunately,
CreateFormFile
doesn't allow specifyinga custom content type or does any content type check but contains a hardcoded generic content type
"application/octet-stream"
The current workaround is to implement a custom
CreateFormFile
replacement with custom content type.What did you expect to see?
I see at least one of possible solutions:
http.DetectContentType
)CreateFormFileWithContentType
)What did you see instead?
Content type is hardcoded in
CreateFormFile
method.The text was updated successfully, but these errors were encountered: