multipart

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BodyFromMap

func BodyFromMap[K comparable, V any](
	m map[K]V,
	opts ...func(Options),
) (string, []byte, error)

BodyFromMap creates a multipart/formdata encoded body by applying a transform function to generate form parts for each item in a map. Configuration functions can be used to set the boundary string and the transformation function.

Returns

string  // the content type for the body
[]byte  // the body
error   // an error (if non-nil, content type and body should be ignored)

Configuration Functions

// to set the boundary string for the body
Boundary(string)

// to set the transformation function for the body
TransformMap(func(K, V) (string, string, []byte, error))

If no boundary is configured, "boundary" is used.

If no transformation function is configured a default transformation is applied (see: TransformMap for details).

If the transformation function returns an error for any item then this will be returned as the error from BodyFromMap; the returned body and content type will be empty and should be ignored.

Example

Demonstrates using the `BodyFromMap` function to create a multipart/formdata encoded body from a map, with a custom boundary string and transformation:

ct, body, err := BodyFromMap(
	map[string]string{"part-id": "content data"},
	Boundary("ABCDEF"),
	MapTransform(func(k, v string) (string, string, []byte, error) {
		return "field-" + k, "filename-" + k, []byte(v), nil
	}),
)

func Boundary

func Boundary(s string) func(Options)

Boundary is a configuration function that sets the boundary string for the multipart body.

If no boundary is set then "boundary" is used.

func TransformMap

func TransformMap[K comparable, V any](fn func(K, V) (string, string, []byte, error)) func(Options)

TransformMap sets the transformation function for the BodyFromMap function.

If no transformation function is set then the default transformation is applied. This will create a part for each key:value pair in the map, with:

  • the key as the fieldname
  • an empty string as the filename
  • an octet-stream ([]byte) containing the string representation of the value as the content

If the supplied transformation function returns an error for any item then this will be returned as the error from BodyFromMap; the returned body and content type will be empty and should be ignored.

Types

type Options

type Options interface {
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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