contenttype

package
v0.0.0-...-9698183 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package contenttype implement encoders and decoders for data by content types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(contentType string, rawBody io.Reader) (any, error)

Decode decodes the data by content type to arbitrary value.

func DecodeXML

func DecodeXML(r io.Reader) (any, error)

DecodeXML decodes an arbitrary XML from a reader stream.

func Encode

func Encode(contentType string, body any) ([]byte, error)

Encode encodes the data by content type.

func EncodeBinary

func EncodeBinary(body any) ([]byte, error)

EncodeBinary encodes the arbitrary value to bytes for binary content type.

func EncodeFormURLEncoded

func EncodeFormURLEncoded(value any, media *highv3.MediaType) (string, error)

EncodeFormURLEncoded encodes the arbitrary value to application/x-www-form-urlencoded content type.

func EncodeMultipartForm

func EncodeMultipartForm(
	bodyData any,
	headers http.Header,
	media *highv3.MediaType,
) ([]byte, string, error)

EncodeMultipartForm encodes the arbitrary value to multipart/form-data content type.

func EncodeText

func EncodeText(body any) ([]byte, error)

EncodeText encodes the arbitrary value to text for text/xxx content type.

func EncodeXML

func EncodeXML(value any) ([]byte, error)

EncodeXML encodes the arbitrary body to XML bytes.

func Write

func Write(writer io.Writer, contentType string, body any) (int, error)

Write encodes the data by content type and writes it to the stream.

func WriteBinary

func WriteBinary(writer io.Writer, body any) (int, error)

WriteBinary encodes the arbitrary value to bytes for binary content type and writes it into the write stream.

func WriteText

func WriteText(writer io.Writer, body any) (int, error)

WriteText encodes the arbitrary value to text for text/xxx content type and write it into the stream.

func WriteXML

func WriteXML(writer io.Writer, value any) (int, error)

WriteXML encodes the arbitrary body to XML and write to a writer.

Types

type DataURI

type DataURI struct {
	MediaType  string
	Parameters map[string]string
	Data       []byte
}

DataURI represents the Data URI scheme

func DecodeDataURI

func DecodeDataURI(input string) (*DataURI, error)

DecodeDataURI decodes data URI scheme data:[<media type>][;<key>=<value>][;<extension>],<data>

type DataURIEncoding

type DataURIEncoding uint8

DataURIEncoding represents a encoding num for the data URI.

const (
	// DataUriASCII represents the ascii encoding for the data URI.
	DataUriASCII DataURIEncoding = iota
	// DataURIBase64 represents the base64 encoding enum for the data URI.
	DataURIBase64
)

type MultipartWriter

type MultipartWriter struct {
	*multipart.Writer
}

MultipartWriter extends multipart.Writer with helpers.

func NewMultipartWriter

func NewMultipartWriter(w io.Writer) *MultipartWriter

NewMultipartWriter creates a MultipartWriter instance.

func (*MultipartWriter) WriteBinary

func (w *MultipartWriter) WriteBinary(
	name string,
	value []byte,
	contentType string,
	headers http.Header,
) error

WriteBinary writes a binary file to the multipart form.

func (*MultipartWriter) WriteDataURI

func (w *MultipartWriter) WriteDataURI(
	name string,
	value any,
	contentType string,
	headers http.Header,
) error

WriteDataURI write a file from data URI string.

func (*MultipartWriter) WriteField

func (w *MultipartWriter) WriteField(fieldName, value string, headers http.Header) error

WriteField calls CreateFormField and then writes the given value.

func (*MultipartWriter) WriteJSON

func (w *MultipartWriter) WriteJSON(fieldName string, value any, headers http.Header) error

WriteJSON calls CreateFormField and then writes the given value with json encoding.

func (*MultipartWriter) WriteXML

func (w *MultipartWriter) WriteXML(fieldName string, value any, headers http.Header) error

WriteXML calls CreateFormField and then writes the given value with XML encoding.

Jump to

Keyboard shortcuts

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