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, httpClient *http.Client, body *bytes.Buffer, r *http.Request, queryParams map[string][]string, storageURL, formDataContentType, authTokenKey string, expectedStatus int) error
SaveFile saves a file to a storage microservice.
Parameters:
- ctx: The context for the request, used to control cancellations and deadlines.
- httpClient: The HTTP client used to send the request to the storage service.
- body: The body of the request, which should contain the file data to be uploaded.
- r: The original HTTP request from which headers will be forwarded.
- queryParams: A map of query parameters to be included 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. This parameter should be set to the content type returned by `multipart.Writer.FormDataContentType()` when creating the multipart body. This tells the server how to parse the form data, including boundary delimiters for different parts of the request.
- authTokenKey: The key for authorization token that will be forwarded from the original request.
Returns:
- An error if the file cannot be saved due to any issue in request preparation, execution, or response handling.
Types ¶
This section is empty.