multipartutil

package
v0.67.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2025 License: MIT Imports: 15 Imported by: 5

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

View Source
const (
	PartTypeFilepath = "filepath"
	PartTypeJSON     = "json"
	PartTypeRaw      = "raw"
)

Variables

This section is empty.

Functions

func NewMultipartReaderForHTTPResponse added in v0.37.0

func NewMultipartReaderForHTTPResponse(resp *http.Response) (*multipart.Reader, error)

func NewReaderBodyBytes

func NewReaderBodyBytes(body []byte, boundary string) *multipart.Reader

func NewRequest

func NewRequest(method, url string, params url.Values, files []FileInfo) (*http.Request, error)

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 FileInfo

type FileInfo struct {
	MIMEPartName string
	Filepath     string
}

FileInfo represents a file for uploading.

type MultipartBuilder

type MultipartBuilder struct {
	Buffer *bytes.Buffer
	Writer *multipart.Writer
}

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

func NewPartAlternativeOrNot(textBody, htmlBody []byte) (Part, error)

NewPartAlternativeOrNot can be used for email bodies which come with text and HTML alternatives.

func NewPartEmpty added in v0.67.3

func NewPartEmpty(ct string) Part

func (*Part) ContentDispositionHeader added in v0.67.3

func (p *Part) ContentDispositionHeader() string

func (Part) FilepathToRaw added in v0.67.3

func (p Part) FilepathToRaw() (Part, error)

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)

func (Part) Write added in v0.67.3

func (p Part) Write(w *multipart.Writer) error

type PartsSet added in v0.67.3

type PartsSet struct {
	ContentType string
	Parts       []Part
}

func NewPartsSet added in v0.67.3

func NewPartsSet(ct string) PartsSet

func NewPartsSetAlternative added in v0.67.3

func NewPartsSetAlternative(text, html []byte) PartsSet

func (*PartsSet) AddMailBody added in v0.67.3

func (ps *PartsSet) AddMailBody(textBody, htmlBody []byte) error

func (*PartsSet) Builder added in v0.67.3

func (ms *PartsSet) Builder(close bool) (MultipartBuilder, error)

func (*PartsSet) Part added in v0.67.3

func (ms *PartsSet) Part() (Part, error)

Part returns the MultipartSimple as a Part. This can be used for creating parts such as `multipart/alternative`.

func (*PartsSet) Strings added in v0.67.3

func (ps *PartsSet) Strings() (ctHeader, body string, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL