Documentation ¶
Overview ¶
Package multipartutil provides helper functionality for using multipart.Writer. Steps are to call NewMultipartBuilder(), write parts, call builder.Close(), and retrieve Content-Type header from builder.Writer.FormDataContentType().
Index ¶
- func NewMultipartReaderForHTTPResponse(resp *http.Response) (*multipart.Reader, error)
- func NewReaderBodyBytes(body []byte, boundary string) *multipart.Reader
- func NewRequest(method, url string, params url.Values, files []FileInfo) (*http.Request, error)
- type FileInfo
- type MultipartBuilder
- func (builder *MultipartBuilder) Close() error
- func (builder *MultipartBuilder) ContentType() string
- func (builder *MultipartBuilder) String() string
- func (builder *MultipartBuilder) WriteFieldAsJSON(partName string, data any, base64Encode bool) error
- func (builder *MultipartBuilder) WriteFieldString(partName string, data string) error
- func (builder *MultipartBuilder) WriteFileHeader(partName string, fileHeader *multipart.FileHeader) error
- func (builder *MultipartBuilder) WriteFilePath(partName, srcFilepath string) error
- func (builder *MultipartBuilder) WriteFilePathPlus(partName, srcFilepath string, base64Encode bool) error
- func (builder *MultipartBuilder) WriteFileReader(partName, filename string, src io.Reader) error
- func (builder *MultipartBuilder) WriteURLValues(params url.Values) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMultipartReaderForHTTPResponse ¶ added in v0.37.0
func NewRequest ¶
NewRequest returns a `*http.Request` for making a request using multipart/form-data. It supports simple strings and files. For more complex field requirements such as JSON body parts that require Content-Type headers and Base64 encoding, use MultipartBuilder directly.
Types ¶
type MultipartBuilder ¶
MultipartBuilder is a multipart helper.
func NewMultipartBuilder ¶
func NewMultipartBuilder() MultipartBuilder
NewMultipartBuilder instantiates a new MultipartBuilder.
func (*MultipartBuilder) Close ¶
func (builder *MultipartBuilder) Close() error
Close closes the `multipart.Writer`.
func (*MultipartBuilder) ContentType ¶
func (builder *MultipartBuilder) ContentType() string
ContentType returns the content type for the `Content-Type` header.
func (*MultipartBuilder) String ¶
func (builder *MultipartBuilder) String() string
String returns the MIME parts as a string.
func (*MultipartBuilder) WriteFieldAsJSON ¶
func (builder *MultipartBuilder) WriteFieldAsJSON(partName string, data any, base64Encode bool) error
WriteFieldAsJSON adds a JSON part.
func (*MultipartBuilder) WriteFieldString ¶
func (builder *MultipartBuilder) WriteFieldString(partName string, data string) error
WriteFieldString adds a text part.
func (*MultipartBuilder) WriteFileHeader ¶
func (builder *MultipartBuilder) WriteFileHeader(partName string, fileHeader *multipart.FileHeader) error
WriteFileHeader adds a file part given a part name and *multipart.FileHeader. See more at http://sanatgersappa.blogspot.com/2013/03/handling-multiple-file-uploads-in-go.html and https://gist.github.com/sanatgersappa/5127317#file-app-go
func (*MultipartBuilder) WriteFilePath ¶
func (builder *MultipartBuilder) WriteFilePath(partName, srcFilepath string) error
WriteFilePath adds a file part given a filename.
func (*MultipartBuilder) WriteFilePathPlus ¶
func (builder *MultipartBuilder) WriteFilePathPlus(partName, srcFilepath string, base64Encode bool) error
WriteFilepathPlus adds a file part given a filename with the Content Type and other associated headers as needed. After builder.Close() has been called, use like `req, err := http.NewRequest("POST", url, builder.Buffer)`. Content-Disposition uses optional attribute as defined here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
func (*MultipartBuilder) WriteFileReader ¶
func (builder *MultipartBuilder) WriteFileReader(partName, filename string, src io.Reader) error
WriteFileReader adds a file part given a filename and `io.Reader`.
func (*MultipartBuilder) WriteURLValues ¶
func (builder *MultipartBuilder) WriteURLValues(params url.Values) error
WriteURLValues writes simple header key value strings using `url.Values` as an input parameter.