multipartutil

package
v0.67.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2025 License: MIT Imports: 14 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 (
	PartTypeJSON     = "json"
	PartTypeFilepath = "filepath"
	PartTypeString   = "string"
)

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() 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 MultipartSimple added in v0.66.1

type MultipartSimple struct {
	Parts []Part
}

func NewMultipartSimple added in v0.66.1

func NewMultipartSimple() MultipartSimple

func (MultipartSimple) Builder added in v0.66.1

func (ms MultipartSimple) Builder(close bool) (MultipartBuilder, error)

func (MultipartSimple) Strings added in v0.66.1

func (ms MultipartSimple) Strings() (ctHeader, body string, err error)

type Part added in v0.66.1

type Part struct {
	Type         string
	Name         string
	Base64Encode bool
	Data         any
	Filepath     string
	String       string
}

Jump to

Keyboard shortcuts

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