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 ¶
- Constants
- 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(ct string) 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
- type Part
- func (p *Part) ContentDispositionHeader() string
- func (p Part) FilepathToRaw() (Part, error)
- func (p Part) HeaderBodyFilepath() (textproto.MIMEHeader, []byte, error)
- func (p *Part) HeaderBodyJSON() (textproto.MIMEHeader, []byte, error)
- func (p Part) HeaderBodyRaw() (textproto.MIMEHeader, []byte, error)
- func (p Part) Write(w *multipart.Writer) error
- type PartsSet
Constants ¶
const ( PartTypeFilepath = "filepath" PartTypeJSON = "json" PartTypeRaw = "raw" )
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(ct string) 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.
type Part ¶ added in v0.66.1
type Part struct { Type string Name string ContentType string ContentDisposition string // short HeaderRaw textproto.MIMEHeader BodyDataFilepath string BodyDataJSON any BodyDataRaw []byte BodyEncodeBase64 bool }
func NewPartAlternativeOrNot ¶ added in v0.67.3
NewPartAlternativeOrNot can be used for email bodies which come with text and HTML alternatives.
func NewPartEmpty ¶ added in v0.67.3
func (*Part) ContentDispositionHeader ¶ added in v0.67.3
func (Part) FilepathToRaw ¶ added in v0.67.3
func (Part) HeaderBodyFilepath ¶ added in v0.67.3
func (p Part) HeaderBodyFilepath() (textproto.MIMEHeader, []byte, error)
HeaderBodyFilepath sets Content-Disposition and Content-Type.
func (*Part) HeaderBodyJSON ¶ added in v0.67.3
func (p *Part) HeaderBodyJSON() (textproto.MIMEHeader, []byte, error)
HeaderBodyJSON adds a JSON part.
func (Part) HeaderBodyRaw ¶ added in v0.67.3
func (p Part) HeaderBodyRaw() (textproto.MIMEHeader, []byte, error)
type PartsSet ¶ added in v0.67.3
func NewPartsSet ¶ added in v0.67.3
func NewPartsSetAlternative ¶ added in v0.67.3
func (*PartsSet) AddMailBody ¶ added in v0.67.3
func (*PartsSet) Builder ¶ added in v0.67.3
func (ms *PartsSet) Builder(close bool) (MultipartBuilder, error)