multipartjuice

package
v1.36.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Description: Package multipartjuice provides a comprehensive set of utilities for handling multipart/form-data requests. It includes functions to create multipart requests, save files to a storage microservice, and perform POST requests using multipart bodies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateMultipartBody

func CreateMultipartBody(ctx context.Context, file []byte, fileName string) (*bytes.Buffer, string, error)

CreateMultipartBody creates a multipart request body and returns it with the appropriate content type.

This function is designed to prepare a multipart form data body for an HTTP request. Multipart form data is commonly used for file uploads, as it allows the client to send files along with other form fields within a single HTTP request. The function returns both the constructed request body and the content type required for the HTTP header.

Parameters:

  • ctx: The context for the operation, which allows for handling cancellations and timeouts.
  • file: A byte slice representing the file to be uploaded. This should contain the raw file data.
  • fileName: The name of the file being uploaded. This will be used to set the `filename` parameter in the `Content-Disposition` header, helping the server to process and store the file correctly.

Returns:

  • *bytes.Buffer: A buffer containing the multipart request body. This is ready to be used as the body of an HTTP POST request.
  • string: The content type for the multipart form data. This includes boundary information that is critical for the server to parse the request properly. The content type should be set as the `Content-Type` header in the HTTP request.
  • error: An error object if any issues occur during the creation of the multipart body. Errors can arise from problems with writing the file data or constructing the multipart structure.

func SaveFile

func SaveFile(ctx context.Context, r *http.Request, params *SaveFileParams) error

SaveFile uploads a file to a storage microservice.

Parameters:

- ctx: The context for the request, used to manage cancellations and deadlines. - r: The original HTTP request from which headers will be forwarded. - params: A pointer to SaveFileParams, encapsulating the following:

  • HTTPClient: The HTTP client used to send the request to the storage service.
  • Body: The body of the request, containing the file data to be uploaded.
  • QueryParams: A map of query parameters to include in the request URL.
  • StorageURL: The URL of the storage microservice where the file will be saved.
  • FormDataContentType: The content type of the multipart form data, including boundary delimiters.
  • ForwardHeaders: A slice of headers to forward to the storage microservice.
  • ExpectedStatus: The expected HTTP status code from the storage microservice.

Returns:

- An error if the file cannot be saved due to issues in request preparation, execution, or response handling.

Types

type SaveFileParams added in v1.35.18

type SaveFileParams struct {
	HTTPClient          *http.Client
	Body                *bytes.Buffer
	QueryParams         map[string][]string
	StorageURL          string
	FormDataContentType string
	ForwardHeaders      []string
	ExpectedStatus      int
}

Jump to

Keyboard shortcuts

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